LeetCode25.K个一组翻转链表

题目:

给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。

k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]

思路:

  • 构建一个伪节点
  • 遍历链表,每 k个循环一次
  • 找到这k个节点的首尾节点
  • 反转链表
  • 将反转后的链表拼接回原来的链表

一种解法:

这种解法,反转链表跟普通的反转链表一样。可以顺便练一下反转链表。
如果追求 通过率,用这种方式好些。

    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null) {
            return null;
        }
        //构建一个伪节点
        ListNode fake = new ListNode(0);
        fake.next = head;

        ListNode prev = fake;
        ListNode end = fake;

        //遍历链表,k个一组,找到首尾链表
        //注意:这里是遍历找尾节点,所以是 end.next!=null
        while (end.next!=null) {

            for (int i=0; i<k && end!=null; i++) {
                end = end.next;
            }
            //如果为null,说明已经到达结尾了
            if (end == null) {
                break;
            }
            //记下翻转首节点、尾节点
            ListNode start = prev.next;
            ListNode next = end.next;
            //注意:断开链表,这个步骤不能少。
            end.next = null;

            //反转链表,并将链表接回原来的链表中
            prev.next = reverse(start);
            start.next = next;
            
            //重置变量,重新迭代
            prev = start;
            end = prev;



        }
        return fake.next;
    }




    /**
     *   反转链表
     */
    public ListNode reverse(ListNode head) {
        //反转链表
        if (head==null) {
            return null;
        }
        ListNode pre = null;
        ListNode curr = head;

        //上一个节点,当前节点,下一个节点
        //记住下一个节点
        while (curr!=null) {
            ListNode next = curr.next;
            //当前节点指向上一个节点
            curr.next = pre;
            
            //向后迭代,当前节点变成上一个节点,下一个节点,变成当前节点
            pre = curr;
            curr = next;
            
        }

        return pre;
    }

另一种解法:


    public ListNode reverseKGroup(ListNode head, int k) {
        //伪节点
        ListNode fake = new ListNode(0);
        fake.next = head;
        ListNode pre = fake;

        while (head != null) {
            ListNode tail = pre;
            // 查看剩余部分长度是否大于等于 k
            for (int i = 0; i < k; ++i) {
                tail = tail.next;
                if (tail == null) {
                    return fake.next;
                }
            }
            ListNode nex = tail.next;
            //反转链表,返回链表的首尾节点
            ListNode[] reverse = myReverse(head, tail);
            head = reverse[0];
            tail = reverse[1];
            // 把子链表重新接回原链表
            pre.next = head;
            tail.next = nex;
            pre = tail;
            head = tail.next;
        }

        return fake.next;
    }



    /**
     *   反转链表
     */
    public ListNode[] myReverse(ListNode head, ListNode tail) {
        ListNode prev = tail.next;
        ListNode p = head;
        while (prev != tail) {
            ListNode nex = p.next;
            p.next = prev;
            prev = p;
            p = nex;
        }
        return new ListNode[]{tail, head};
    }


}



posted on   乐之者v  阅读(5)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-01-19 Spring事务管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示