反转整个链表
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
if(pHead==NULL){
return NULL;
}
struct ListNode* pre=NULL;
struct ListNode* cur=pHead;
while(cur!=NULL){
struct ListNode* temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
return pre;
}理解
- 让
temp
保存cur->next
的地址 pre
第一次要为NULL
- 让
反转整个链表
https://tsy244.github.io/2023/04/10/算法/数据结构/反转整个链表/