代码:
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* @author Senky
* @date 2023.03.17
* @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode* prev = NULL;
struct ListNode* curr = pHead;
struct ListNode* next;
//退出循环后curr指向NULL
while(curr != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
图解:
