题目不贴了leetcode206
/**
* 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==nullptr){
return nullptr;
}
ListNode* behind = head;
ListNode* tou =head;
while(behind -> next)
behind = behind -> next;
tou = behind;
while(behind != head){
ListNode* before = head;
while(before -> next != behind)
before = before -> next;
behind -> next = before;
before -> next = NULL;
behind = before;
}
return tou;
}
};
记录头节点位置,俩指针负责转圈,干就完了!