2.3

来源:互联网 发布:利达消防主机编程 编辑:程序博客网 时间:2024/06/09 19:59
Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e

Result: nothing is returned, but the new linked list looks like a->b->d->e


编程之美上的题目,只需要删除该节点的后一个节点,并将后一个节点的data拷贝过来。

void delete_mid(Node* mid){if(mid==NULL) return;Node* p=mid->next;if(p==NULL) return;mid->next=p->next;mid->data=p->data;delete p;}
注意:这里不能处理最后一个节点的情况

原创粉丝点击