
先找到m~n的区间,然后区间内反转,然后头尾结点重新指向
注意不能返回head,应该返回dummy->next
反例 [3,5] m=1 n=2
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public:ListNode* reverseBetween(ListNode* head, int m, int n) {if(m==n) return head;ListNode * dummy = new ListNode(-1);dummy->next = head;ListNode* a = dummy, *b, *c = dummy, *d,*p,*q;for(int i=0;i<m-1;i++) a = a->next;for(int i=0;i<n;i++) c = c->next;b = a->next;d = c->next;for(p=b,q=b->next;q!=d;){ListNode* e = q->next;q->next = p;p = q;q = e;}a->next = c; b->next = d;return dummy->next;}};
