/
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode next;
* };
/
struct ListNode reverseList(struct ListNode head)
{
struct ListNode prev = NULL;
struct ListNode curr = head;
struct ListNode *next = NULL;
while (curr != NULL) {
next = curr->next; // 先保存下一个节点
curr->next = prev; // 将当前节点的next指向prev
prev = curr; // 移动prev和curr
curr = next;
}
return prev; // 返回新的头结点
}