思路:
枚举每个节点,然后后一个的next指向前一个。
代码:
/**
* 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 *a = NULL,*b = head;
while(b){
auto c = b->next;
b->next = a;
a = b;
b = c;
}
return a;
}
};
评论区