Note:
开个虚拟头结点,然后自己画个图模拟一遍过程即可
代码如下:
/**
* 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) {
if(!head) return NULL;
auto dummy = new ListNode(-1), cur = dummy;
dummy -> next = head;
auto p = dummy -> next;
while(p){
auto q = p -> next;
p -> next = cur;
cur = p, p = q;
}
dummy -> next -> next = NULL;
return cur;
}
};