zhangas

Pay more attention

0%

链式存储结构

206. Reverse Linked List

How to exchange the nodes without changing their values?
from the side to another: u could see[pre, cur, cur->next]
it’s easy to know when “cur” gets NULL, the “pre” is at the end of linked list.
we could store “cur->next” in temp then makes “cur” at front of “pre”
so now “pre” turned to “cur” and “cur” turned to the temp’s value(used to be “cur->next”)

1
2
3
4
5
6
7
8
9
10
11
ListNode* reverseList(ListNode* head) {
ListNode* pre=NULL;
ListNode* cur=head;
while(cur!=NULL){
auto t=cur->next;
cur->next=pre;
pre=cur;
cur=t;
}
return pre;
}