函数思想
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//存放后面未翻转节点
ListNode *succ = NULL;
ListNode *reverseN(ListNode *head,int n)
{
if(n==1)
{
succ = head->next;
return head;
}
//翻转n个节点,直到剩余一个节点
ListNode*ret = reverseN(head->next,n-1);
head->next->next = head;
head->next = succ;
return ret;
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m==1)
return reverseN(head,n);
//进行移动
head->next = reverseBetween(head->next,m-1,n-1);
return head;
}
};
移动时 head->next = reverseBetween(head->next);