【Hot100】LeetCode—25. K 个一组翻转链表


1- 思路

  • 1- 定义两个指针
    • pre 指针:指向所有已经翻转的链表的尾部结点
    • end 指针:指向所有待翻转结点的最后一个结点
  • 2- 两个指针定位后——> 处理断链逻辑,断链需要记录 end 后的节点,便于下次翻转
  • 3- 断链之后 ——> 执行递归翻转链表逻辑
    • 之后更新 pre 为当前 end

2- 实现

⭐25. K 个一组翻转链表——题解思路

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        // 定义 dummyHead
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        ListNode end = dummyHead;
        
        // 遍历
        while(end.next!=null){

            //1. 定位 end
            for(int i = 0 ;i<k && end!=null;i++){
                end = end.next;
            }
            if(end==null){
                break;
            }

            // 2.记录 next
            ListNode tmp = end.next;
            // 断链
            ListNode start = pre.next;
            end.next = null;
            pre.next = null;

            // 翻转
            pre.next = reverseList(start);

            // 更新
            start.next = tmp;
            pre = start;
            end = start;
        }
        return dummyHead.next;
    }

    public ListNode reverseList(ListNode head){
        if(head==null || head.next == null){
            return head;
        }
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }
}

3- ACM 实现

public class kreverseList {


    static class ListNode{
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int x){
            val = x;
        }
    }

    public static ListNode reverseKGroup(ListNode head,int k){
        // 虚拟头结点
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        ListNode end = dummyHead;

        // 2. 翻转
        while(end.next!=null){

            // 2.1 定位 end
            for(int i = 0;i<k && end!=null;i++){
                end = end.next;
            }
            if(end==null) {
                break;
            }

            // 2.2 断链 + 记录
            ListNode tmp = end.next;
            ListNode start = pre.next;
            end.next = null;
            pre.next = null;

            // 2.3 翻转
            pre.next = reverList(start);

            // 2.4 更新
            start.next = tmp;
            pre = start;
            end = start;
        }
        return dummyHead.next;
    }

    public static ListNode reverList(ListNode head){
        if(head==null || head.next==null){
            return head;
        }
        ListNode cur = reverList(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }


    public static void main(String[] args) {
        // 构造链表
        Scanner sc = new Scanner(System.in);
        ListNode head= null;
        ListNode tail = null;
        System.out.println("输入链表长度");
        int n = sc.nextInt();
        for(int i = 0 ;i < n;i++){
            ListNode newNode = new ListNode(sc.nextInt()) ;
            if(head==null){
                head = newNode;
                tail = newNode;
            }else{
                tail.next = newNode;
                tail = tail.next;
            }
        }
        System.out.println("输入k");
        int k = sc.nextInt();
        ListNode forRes = reverseKGroup(head,k);

        while(forRes!=null){
            System.out.print(forRes.val+" ");
            forRes = forRes.next;
        }
    }
}

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值