zoukankan      html  css  js  c++  java
  • LeetCode之“链表”:Reverse Nodes in k-Group

      题目链接

      题目要求:

      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

      这道题个人想法是将链表分段求逆,然后再合并。具体程序如下(有点冗余,有些代码可以合并):

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* reverseList(ListNode* head, int k)
    12     {
    13         if(!head || !head->next)
    14             return head;
    15             
    16         int len = 0;
    17         ListNode *start = head;
    18         while(start)
    19         {
    20             len++;
    21             start = start->next;
    22             if(len == k)
    23                 break;
    24         }
    25         if(len < k)
    26             return head;
    27         
    28         ListNode *postList = start;
    29         start = head;
    30         ListNode *prev = nullptr;
    31         while(start != postList)
    32         {
    33             ListNode *next = start->next;
    34             start->next = prev;
    35             prev = start;
    36             start = next;
    37         }
    38         
    39         start = prev;
    40         while(start->next)
    41             start = start->next;
    42         start->next = postList;
    43         
    44         return prev;
    45     }
    46 
    47     ListNode* reverseKGroup(ListNode* head, int k) {
    48         ListNode *dummy = new(nothrow) ListNode(INT_MIN);
    49         assert(dummy);
    50         dummy->next = head;
    51         ListNode *preNode = dummy;
    52         while(true)
    53         {
    54             preNode->next = reverseList(head, k);
    55             head = preNode->next;
    56             int cnt = 0;
    57             while(head)
    58             {
    59                 cnt++;
    60                 head = head->next;
    61                 preNode = preNode->next;
    62                 if(cnt == k)
    63                     break;
    64             }
    65             
    66             if(cnt < k)
    67             {
    68                 head = dummy->next;
    69                 delete dummy;
    70                 dummy = nullptr;
    71                 return head;
    72             }
    73         }
    74     }
    75 };
  • 相关阅读:
    分享memcache和memcached安装过程(转)
    ios画图总结
    mac os下通过命令行的方式编译c++代码并在xcode里引用
    ubuntu访问windows共享文件夹
    为iphone 及iphone simulator编译boost库
    ubuntu 11.10 x64 安装oracle 11gR2时碰到的问题及解决方法
    模拟器与真机下ffmpeg的编译方法(总结版)
    ios 在View里绘图
    memcached1.4.4在ubuntu下编译的注意事项
    Google Code 服务试用
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4603881.html
Copyright © 2011-2022 走看看