首页 > 其他 > 详细

Reverse Nodes in k-Group

时间:2015-10-17 17:39:15      阅读:117      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!