题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
反正指针指向
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param pHead ListNode类 * @return ListNode类 */ struct ListNode* ReverseList(struct ListNode* pHead ) { // write code here if(pHead==NULL || pHead->next ==NULL) return pHead; struct ListNode* pre=pHead;//用于指向前一个节点 struct ListNode* pCur=pHead->next;//辅助指针,用于指向当前节点 pHead->next=NULL; struct ListNode* tmp=pCur;//用于保存当前节点的下一个节点地址 while(pCur->next!=NULL) { tmp = pCur->next;//保存当前节点的下一个节点地址 pCur->next=pre;//反转当前节点的指向 pre=pCur;// 前一个节点前进到当前节点 pCur =tmp;//当前节点前进一个节点 } pCur->next=pre; return pCur; }