标签:
1 class Solution { 2 public: 3 ListNode* reverseKGroup(ListNode* head, int k) { 4 if(head==NULL||k<=1)return head; 5 ListNode prehead(0); 6 prehead.next=head; 7 ListNode *pre=&prehead,*keep,*first; 8 while(head!=NULL) 9 { 10 int n=k; 11 while(n--) 12 { 13 if(head==NULL) return prehead.next; 14 else 15 head=head->next; 16 17 } 18 //reverse(pre,p); 19 first=pre->next->next; 20 keep=pre->next; 21 while(first!=head) 22 { 23 24 keep->next=first->next; 25 first->next=pre->next; 26 pre->next=first; 27 first=keep->next; 28 } 29 30 pre=keep; 31 32 } 33 return prehead.next; 34 } 35 };
标签:
原文地址:http://www.cnblogs.com/daocaorenblog/p/4887674.html