• [LeetCode] 25. 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.

    Follow up:

    • Could you solve the problem in O(1) extra memory space?
    • You may not alter the values in the list's nodes, only nodes itself may be changed.

    Example 1:

    Input: head = [1,2,3,4,5], k = 2
    Output: [2,1,4,3,5]
    

    Example 2:

    Input: head = [1,2,3,4,5], k = 3
    Output: [3,2,1,4,5]
    

    Example 3:

    Input: head = [1,2,3,4,5], k = 1
    Output: [1,2,3,4,5]
    

    Example 4:

    Input: head = [1], k = 1
    Output: [1]

    Constraints:

    • The number of nodes in the list is in the range sz.
    • 1 <= sz <= 5000
    • 0 <= Node.val <= 1000
    • 1 <= k <= sz

    每k个一组翻转链表。题意是请将input中每K个node进行翻转。递归的思路是用cur指针遍历input,然后用count记录遍历的node个数。因为cur是从head开始的,所以当count == K的时候,cur已经到了第K + 1个node的地方了。此时用递归调用,反转K个node。若不足K个的时候,不会进行翻转。

    时间O(n)

    空间O(n)

    JavaScript实现

     1 /**
     2  * @param {ListNode} head
     3  * @param {number} k
     4  * @return {ListNode}
     5  */
     6 var reverseKGroup = function(head, k) {
     7     // corner case
     8     if (head === null || head.next === null) {
     9         return head;
    10     }
    11 
    12     // normal case
    13     let count = 0;
    14     let cur = head;
    15     while (cur !== null && count != k) {
    16         cur = cur.next;
    17         count++;
    18     }
    19     if (count === k) {
    20         cur = reverseKGroup(cur, k);
    21         while (count-- > 0) {
    22             let next = head.next;
    23             head.next = cur;
    24             cur = head;
    25             head = next;
    26         }
    27         head = cur;
    28     }
    29     return head;
    30 };

    迭代的思路是设置两个pointer,start和end,卡住需要翻转的部分,同时需要记录一个pre和一个nextStart这样翻转完了之后不至于丢失前后的node。一个很好的图示,帮助理解。思路如下,

    步骤分解:

    • 链表分区为已翻转部分+待翻转部分+未翻转部分
    • 每次翻转前,要确定翻转链表的范围,这个必须通过 k 此循环来确定
    • 需记录翻转链表前驱和后继,方便翻转完成后把已翻转部分和未翻转部分连接起来
    • 初始需要两个变量 pre 和 end,pre 代表待翻转链表的前驱,end 代表待翻转链表的末尾
    • 经过k此循环,end 到达末尾,记录待翻转链表的后继 next = end.next
    • 翻转链表,然后将三部分链表连接起来,然后重置 pre 和 end 指针,然后进入下一次循环
    • 特殊情况,当翻转部分长度不足 k 时,在定位 end 完成后,end==null,已经到达末尾,说明题目已完成,直接返回即可
    • 时间复杂度为 O(n*K)O(n∗K) 最好的情况为 O(n) 最差的情况为 O(n^2)
    • 空间复杂度为 O(1) 除了几个必须的节点指针外,我们并没有占用其他空间

    作者:reals
    链接:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    时间O(n)

    空间O(1) - 题目要求不使用额外空间

    Java实现

     1 class Solution {
     2     public ListNode reverseKGroup(ListNode head, int k) {
     3         ListNode dummy = new ListNode(0);
     4         dummy.next = head;
     5         ListNode pre = dummy;
     6         ListNode end = dummy;
     7         while (end != null) {
     8             for (int i = 0; i < k && end != null; i++) {
     9                 end = end.next;
    10             }
    11             if (end == null) {
    12                 break;
    13             }
    14             ListNode nextStart = end.next;
    15             ListNode start = pre.next;
    16             end.next = null;
    17             pre.next = reverse(start);
    18             // 保持下次循环一致的位置
    19             start.next = nextStart;
    20             pre = start;
    21             end = pre;
    22         }
    23         return dummy.next;
    24     }
    25 
    26     private ListNode reverse(ListNode head) {
    27         ListNode pre = null;
    28         ListNode cur = head;
    29         while (cur != null) {
    30             ListNode next = cur.next;
    31             cur.next = pre;
    32             pre = cur;
    33             cur = next;
    34         }
    35         return pre;
    36     }
    37 }

    LeetCode 题目总结 

  • 相关阅读:
    SpringBoot上传图片
    Java工具类(4) ------>运用easyexcel生成Excel表格
    SpringBoot(8) ------>集成SpringSecurity与Jwt
    docker_进阶
    jenkins基础
    C# 8 using declarations
    Sysmetric encryption and decryption via AES
    C# class implementation order of constructors include static constructor,constructor without modifiers and parameters, constructor with modifiers and parameters
    C# dictionary keys case insensitive
    Centos7安装Docker及运行hello-world
  • 原文地址:https://www.cnblogs.com/cnoodle/p/11817279.html
Copyright © 2020-2023  润新知