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.
  • 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
  • 地址

问题分析

  • 该题可以看作是 LeetCode 24. Swap Nodes in Pairs 的进阶题目,只不过 24 是 两个两个进行 swap,而该题是每k个一组进行reverse,若最终不足 k个,则保持原顺序
  • 同样可以用 dummy 和 递归两种方法
  • dummy 法
    • 先统计整体链表长度,然后每 k 个一组进行 reverse ,当不足 k 个时,便直接返回。
    • 注意,该实现中,用到的 reverse 方法 类似于 LeetCode 92. Reverse Linked List II 法。 是一种 “头插法”
    • 因为需要一个节点保存已经反转好的链表的尾部,所以需要用 dummy节点,处理初始情况。
  • 递归法(虽然空间复杂度不满足该题要求)
    • 具体见实现

经验教训

代码实现

  • dummy (满足题目要求)
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null ) {
            return head;
        }
        ListNode curNode = head;
        int length = 0;
        //计算链表长度
        while (curNode != null) {
            curNode = curNode.next;
            ++length;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        //已经反转完成的链表的尾部用 preList指示
        ListNode preList = dummy;
        //curNode用于指示待反转的的第一个节点
        curNode = head;
        while (length >= k) { //只要还存在大于等于 k 个,便可以反转
            //对k个进行反转
            for (int i = 1; i < k; i++) {
                ListNode nextNode = curNode.next;
                curNode.next = nextNode.next;
                nextNode.next = preList.next;
                preList.next = nextNode;
            }
            //迭代,反转下k个
            preList = curNode;
            curNode = curNode.next;
            length -= k;
        }
        return dummy.next;
    }
  • 递归
     public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null || k < 2) {
            return head;
        }
        ListNode curNode = head;
         int count = 0;
         while (curNode != null && count < k) {
             curNode = curNode.next;
             ++count;
         }
         //当前链表长度小于 k ,那么无需反转,直接返回
         if (count < k) {
             return head;
         }
         //将 [k + 1...length] 之后的递归反转
         ListNode resList = reverseKGroup(curNode, k);
         //将前k个进行反转
         ListNode preNode = null;
         curNode = head;
         while (k-- > 0) {
             ListNode nextNode = curNode.next;
             curNode.next = preNode;
             preNode = curNode;
             curNode = nextNode;
         }
         //两部分相连
         head.next = resList;
         return preNode;
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值