本文介绍了如何从链接列表中删除中间节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个单向链表。如果我想从这个链表中删除一个已知的元素,我该怎么办?
I have a singly linked list. If I want to delete an known element from this linked list, what can I do?
例如:节点*头; (44)节点*尾巴; (39)
For example:Node* head; (44)Node* tail; (39)
链表:44 27 59 13 45 39我们想从中删除45。并获得:44 27 59 13 39
linked list: 44 27 59 13 45 39we want to delete 45 from it. and get: 44 27 59 13 39
我只是想通了,从列表中删除第一个元素(如元素(需要被删除)是列表的第一个元素)。我:头=流浆>旁边;
I only figured out that delete first element from list(if element(need to be removed) is first element of the list).I got: head = head-> next;
如何从列表中删除中间节点?
How to remove intermediate node from list?
推荐答案
这个伪code可以帮助你: -
This pseudo code might help you :-
void remove(int key) {
Node* p = head->next;
Node*prev = head;
while(p!=NULL) {
if(p->data==key) {
prev->next = p->next;
free(p);
break;
}
prev = p;
p = p->next;
}
}
这篇关于如何从链接列表中删除中间节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!