【发布时间】:2016-09-04 14:01:15
【问题描述】:
我遍历了一个链表,最终在我修改了 head 中的一些元素之间达到了 NULL。如何通过这些修改取回头指针。
Node* temp=head;
while(head&&head->next){
head=head->next->next;
}
我希望将链表修改为带有备用节点的新链表。所以在此之后我怎样才能取回新的头指针。
编辑:
ListNode* temp=head,*new1=head;
while(head!=NULL&&head->next){
new1->next=head->next->next;
head->next=head->next->next;
new1=new1->next;
}
//temp=head;
return new1;
【问题讨论】:
-
答案只能和问题一样好。发布Minimal Complete Verifiable Example 肯定会改善问题。
-
只使用临时节点而不是head进行遍历,这样head保持不变
-
我希望在该列表本身中对其进行修改
-
您希望
while循环能为您做什么?你如何检查它是否做了正确的事情?如果您考虑可以添加哪些代码来仔细检查您打算发生的事情是否确实发生了,您通常可以回答自己的问题。 -
while循环将head的备用节点添加到新节点。
标签: c++ c linked-list singly-linked-list