标签:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseBetween(ListNode* head, int m, int n) { 12 if(m==n) return head; //经常弄成赋值,找bug找半天 13 int i=1; 14 ListNode prehead(0); // 注意进行初始化 15 prehead.next=head; 16 ListNode *mpre,*p; 17 18 mpre=&prehead; 19 20 while(i<m) 21 { 22 if(head&&head->next) 23 { 24 head=head->next; 25 mpre=mpre->next; 26 i++; 27 } 28 else return prehead.next; 29 30 } 31 32 p=head->next; 33 34 while(i<n) 35 { 36 if(p) 37 { 38 p=head->next; 39 head->next=p->next; 40 p->next=mpre->next; 41 mpre->next=p; 42 i++; 43 } 44 else break; 45 } 46 47 return prehead.next; 48 49 } 50 51 };
标签:
原文地址:http://www.cnblogs.com/daocaorenblog/p/4886159.html