首页 > 其他 > 详细

LeetCode - Reverse Nodes in k-Group

时间:2014-02-27 21:10:05      阅读:443      评论:0      收藏:0      [点我收藏+]

Reverse Nodes in k-Group

2014.2.26 23:37

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Solution:

  The idea is simple for this problem: find the next k-group, reverse it and put it back.

  All you have to worry about is careful coding.

  Total time complexity is O(n). Space complexity is O(1).

Accepted code:

bubuko.com,布布扣
 1 // 1RE, 1AC, good.
 2 /**
 3  * Definition for singly-linked list.
 4  * struct ListNode {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     ListNode *reverseKGroup(ListNode *head, int k) {
13         if (head == nullptr || k < 2) {
14             // no change at all
15             return head;
16         }
17         ListNode *bh, *h, *t, *at;
18         
19         bh = nullptr;
20         h = head;
21         int i;
22         while (true) {
23             if (h == nullptr) {
24                 break;
25             }
26             t = h;
27             for (i = 1; i < k; ++i) {
28                 t = t->next;
29                 if (t == nullptr) {
30                     break;
31                 }
32             }
33             if (i == k) {
34                 at = t->next;
35                 t->next = nullptr;
36                 reverseList(h, t);
37                 if (bh != nullptr) {
38                     // put the segment in place again
39                     bh->next = h;
40                     t->next = at;
41                 } else {
42                     // the first group will change the head of the list
43                     head = h;
44                     t->next = at;
45                 }
46                 // find the next group
47                 bh = t;
48                 h = at;
49             } else {
50                 break;
51             }
52         }
53         
54         
55         return head;
56     }
57 private:
58     void reverseList(ListNode *&h, ListNode *&t) {
59         if (h == nullptr || t == nullptr) {
60             // impossible
61             return;
62         }
63         if (h == t) {
64             // only one node
65             return;
66         }
67         
68         ListNode *new_h, *new_t;
69         ListNode *ptr, *tmp;
70         
71         new_h = nullptr;
72         ptr = h;
73         while (ptr != nullptr) {
74             if (new_h == nullptr) {
75                 new_h = new_t = ptr;
76                 ptr = ptr->next;
77                 new_h->next = nullptr;
78             } else {
79                 tmp = ptr->next;
80                 ptr->next = new_h;
81                 new_h = ptr;
82                 ptr = tmp;
83             }
84         }
85         
86         h = new_h;
87         t = new_t;
88     }
89 };
bubuko.com,布布扣

LeetCode - Reverse Nodes in k-Group,布布扣,bubuko.com

LeetCode - Reverse Nodes in k-Group

原文:http://www.cnblogs.com/zhuli19901106/p/3570498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!