题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
原地翻转法
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode* pre=NULL;
struct ListNode* cur=pHead;
struct ListNode* next=cur->next;
if(!pHead){
return pHead;
}
//原地反转法
while(cur){
cur->next=pre;
pre=cur;
cur=next;
next=next->next;
}
return pre;
}