简单。就不解释了...(貌似内容无中文容易被CU判定为advertise给删掉...所以说这么句废话...)
-
/**
-
* 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;
-
int cnt=m;
-
ListNode *pre=NULL;
-
ListNode *h=head;
-
while(h&&--cnt) {pre=h;h=h->next;}
-
ListNode *n1=h;
-
ListNode *n2=h->next;
-
ListNode *n3=NULL;
-
n=n-m;
-
while(n1&&n2&&n--)
-
{
-
n3=n2->next;
-
n2->next=n1;
-
n1=n2;
-
n2=n3;
-
}
-
h->next=n3;
-
if(pre) pre->next=n1;
-
else head=n1;
-
return head;
-
}
-
};
阅读(174) | 评论(0) | 转发(0) |