思路:
用三个指针来维护节点
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return head;
ListNode* a = nullptr;
auto b = head;
while(b){
auto c = b->next;
b->next = a;
a = b,b = c;
}
return a;
}
};
评论区