如何在链表中遍历后得到列表的头
how to get head of the list after traversal in Linked lists?
我遍历了一个链表,结果在head中达到NULL。在这中间,我修改了head中的一些元素。我该如何在修改后取回指针。
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;
我想你的意思是:
Node* temp = head;
while(temp && temp->next){
temp = temp->next->next;
}
这样,你总是有头脑的。
一种方法是使用不同的指针遍历列表,而不使用head
。
另一种方法是在完成后恢复head
。您的代码似乎表明这是可能的,因为在进入循环之前,您已经将head
存储在temp
中。
head = temp;
new1 = temp;
相关文章:
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 是否有任何优雅的方式来遍历元素位置可以更改的列表?
- 遍历列表包含排除最后一个元素的内容
- 自定义链表const_iterator无法遍历列表的非const实例
- Node中的子节点列表在C++中的BFS遍历过程中丢失
- 迭代是否遍历整个列表
- 遍历自定义列表
- 使用引用遍历链接列表不起作用
- 如果使用迭代器,如何解决遍历列表的问题
- c++列表遍历和初始化
- 如何在 c++ 列表中遍历固定数量的元素
- 在c++中遍历一个列表并在另一个列表中搜索每个元素
- 如何在链表中遍历后得到列表的头
- 遍历指向错误元素的列表
- 遍历矢量 c++ 中的列表
- 如何遍历非默认构造类的提升 MPL 列表
- 当此列表包含父列表时,如何遍历列表,它使用标准列表
- 遍历标准:列表<wstring>
- 遍历链接列表时出现分段错误
- JSONcpp遍历对象中的列表