zoukankan      html  css  js  c++  java
  • LeetCode

    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:

     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 };
  • 相关阅读:
    xCode中怎样保存自己的代码块
    2015-03-13---抽象工厂(附代码),
    java nio 缓冲区(一)
    MFC获取各个窗体(体)之间的指针(对象)
    自己动手写神经网络,自己真的能够动手写神经网络嘛?
    Android招財进宝手势password的实现
    QQ三方登录
    UVA 10561
    Vagi单点登录1.0
    《反脆弱》:软件业现成的鲁棒性(Robust)换了个说法变成了作者的发明,按作者的理论推导出许多可笑愚蠢的原则来
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3570498.html
Copyright © 2011-2022 走看看