码迷,mamicode.com
首页 > 其他好文 > 详细

Reverse Nodes in k-Group

时间:2015-10-17 17:39:54      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

 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 };

 

Reverse Nodes in k-Group

标签:

原文地址:http://www.cnblogs.com/daocaorenblog/p/4887674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!