- 思路
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
ListNode* pre = nullptr;
ListNode* temp = nullptr;
ListNode* cur = head;
while (cur)
{
temp = cur->next;// 保存一下 cur的下一个节点,因为接下来要改变cur->next
cur->next = pre;// 翻转操作 更改cur->next的指向
pre = cur;//更新pre and cur
cur = temp;
}
return pre;
}
};