现在的位置: 首页 > 综合 > 正文

2.3

2013年08月21日 ⁄ 综合 ⁄ 共 390字 ⁄ 字号 评论关闭

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;
}

注意:这里不能处理最后一个节点的情况

抱歉!评论已关闭.