zoukankan      html  css  js  c++  java
  • 【LeetCode】025. 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.

    k is a positive integer and is less than or equal to the length of the linked 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

    题解:

      反转链表最适合递归去解决。首先想到的是判断当前链表是否有k个数可以反转,如果没有k个数则返回头结点,否则反转该部分链表并递归求解后来的反转链表部分。为数不多的Hard 题AC

    Solution 1

     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* reverseKGroup(ListNode* head, int k) {
    12         ListNode* cur = head, *tail = nullptr;
    13         for(int i = 0; i < k; ++i) {
    14             if (!cur)
    15                 return head;
    16             if (i == k - 1)
    17                 tail = cur;
    18             cur = cur->next;
    19         }
    20         
    21         reverse(head, tail);
    22         head->next = reverseKGroup(cur, k);
    23         
    24         return tail;
    25     }
    26     
    27     void reverse(ListNode* head, ListNode* tail) {
    28         if (head == tail)
    29             return;
    30         ListNode* pre = head, *cur = pre->next;
    31         while(cur != tail) {
    32             ListNode* tmp = cur->next;
    33             cur->next = pre;
    34             pre = cur;
    35             cur = tmp;
    36         }
    37         cur->next = pre;
    38     }
    39 };

      简化版:

     1 class Solution {
     2 public:
     3     ListNode* reverseKGroup(ListNode* head, int k) {
     4         ListNode *cur = head;
     5         for (int i = 0; i < k; ++i) {
     6             if (!cur) return head;
     7             cur = cur->next;
     8         }
     9         ListNode *new_head = reverse(head, cur);
    10         head->next = reverseKGroup(cur, k);
    11         return new_head;
    12     }
    13     ListNode* reverse(ListNode* head, ListNode* tail) {
    14         ListNode *pre = tail;
    15         while (head != tail) {
    16             ListNode *t = head->next;
    17             head->next = pre;
    18             pre = head;
    19             head = t;
    20         }
    21         return pre;
    22     }
    23 };

    转自:Grandyang

      首先遍历整个链表确定总长度,如果长度大于等于k,那么开始反转这k个数组成的部分链表。交换次数为k-1次,反转后,更新剩下的总长度,迭代进行。

    Solution 2

     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* reverseKGroup(ListNode* head, int k) {
    12         ListNode dummy(-1);
    13         dummy.next = head;
    14         ListNode* pre = &dummy, *cur = pre;
    15         int len = 0;
    16         while (cur->next) {
    17             ++len;
    18             cur = cur->next;
    19         }
    20         while (len >= k) {
    21             cur = pre->next;
    22             for (int i = 0; i < k - 1; ++i) {
    23                 ListNode* tmp = cur->next;
    24                 cur->next = tmp->next;
    25                 tmp->next = pre->next;
    26                 pre->next = tmp;
    27             }
    28             pre = cur;
    29             len -= k;
    30         }
    31         
    32         return dummy.next;
    33     }
    34 };
  • 相关阅读:
    设计模式---行为变化模式之命令模式(Command)
    设计模式---数据结构模式之职责链模式(Chain of Responsibility)
    设计模式---数据结构模式之迭代器模式(Iterate)
    WinCE全屏手写输入法
    .net下所有DLL(API)查询,转换C#代码
    在线cron表达式生成器
    完全卸载vs2013、vs2015的方法
    java微信 客服接口-发消息 中文乱码
    【路由达人】简单两步搞定小米路由新增功能-DDNS(解析域名地址转向在线工具)
    微信公众平台开发入门教程
  • 原文地址:https://www.cnblogs.com/Atanisi/p/8647712.html
Copyright © 2011-2022 走看看