Leetcode上List问题的总结(持续更新)

本文详细解析了链表中的三个经典算法问题:检测链表是否有环并返回环开始的节点;返回两个链表相交的节点;以及链表元素的重新排序。提供了清晰的思路分析与实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

———-检测链表是否有环,并返回环开始的节点
题目142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

 解法:参考了leetcode的某个大神的解法。通过快慢指针。假设快指针的速度为2S,慢指针的速度为S,因为花相同的时间在B点相遇,因此有等式 : (A+B+N*LOOP) / 2S = (A+B) / S ,又因为B+C=LOOP,因此可得 A=(n-1LOOP + C。 
 我们不关心(N-1) LOOP,因此可以将系数n-1看作为0,所以最后必满足A=C。

这里写图片描述
代码:

  public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null || head.next.next == null){
            return null;
        }
        ListNode fast = head.next.next;
        ListNode slow = head.next;
        // 找相遇点
        while(fast != slow){
            if(fast.next == null || fast.next.next == null){
                return null;
            } 
            fast = fast.next.next;
            slow = slow.next;
        }
        // 找到相遇点
        slow = head;
        //因为A=C,所以从头开始找
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

———-返回两个链表相交的节点
题目160. Intersection of Two Linked Lists

A:          a1 → a2
                      ↘
                          c1 → c2 → c3
                      ↗            
B:     b1 → b2 → b3

网上很多方法都是要先求出链表A和链表B的长度的差值。leetcode上有一个大神提出很妙的方法,可以不需要知道长度的差值。

思路大概是这样的。让指针A从headA一直遍历完整个链表(a1——c3)直到为空,然后将指针A重置到b1(headB),与此同时,指针B也从headB一直遍历整个链表(b1——c3),直到为空,然后将指针B重置到a1(headA)。假设链表A比链表B长,那么指针B就会先走完,回到headA,继续走,后面指针A也会最终走完,回到headB,继续走,最终相遇。
这个思路的要点就是保证两者同一时间开始,以相同速度走。因此路程相同。

代码:

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null ){
            return null;
        }
        ListNode a = headA;
        ListNode b = headB;
        while(a != b){
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }

———-返回两个链表相交的节点
题目143.Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

思路:
1. 找到中间点(利用快慢指针)
2. 链表反转
3. 结合

public void reorderList(ListNode head) {
        if(head == null){
            return;
        }
        ListNode fast = head.next;
        ListNode slow = head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode newNode = reverseList(slow.next);
        slow.next=null;
        merge(head,newNode);

    }

    public ListNode reverseList(ListNode head){
        if(head == null){
            return null;
        }
        ListNode newHeader = new ListNode(0);
        ListNode cur = head;
        newHeader.next = head;
        while(cur != null && cur.next != null){
            ListNode n = cur.next;
            cur.next = n.next;
            n.next = newHeader.next;
            newHeader.next = n;
        }
        return newHeader.next;
    }

     public ListNode merge(ListNode h1, ListNode h2){
        ListNode res = new ListNode(0);
        ListNode p = res;
        while(h1!=null||h2!=null){
            if(h1!=null){
                p.next=h1;
                p=p.next;
                h1=h1.next;
            }
            if(h2!=null){
                p.next=h2;
                p=p.next;
                h2=h2.next;
            }

        }
        return res.next;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值