第10题 Linked List Cycle II

本文介绍了一种在不使用额外空间的情况下检测并找到循环链表起始节点的方法,详细解释了算法原理和实现过程。

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Follow up:

Can you solve it without using extra space?

Solution:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */ 
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head==null) return null;
        ListNode ptr1=head, ptr2=head;
       while(ptr1!=null && ptr2!=null){
         if(ptr1.next==null || ptr2.next==null || ptr2.next.next==null) return null;
         ptr1=ptr1.next;
         ptr2=ptr2.next.next;
         if(ptr1==ptr2)
            break;
       }
       ptr1=head;
       while(ptr1!=ptr2){
           ptr1=ptr1.next;
           ptr2=ptr2.next;
       }
       return ptr1;
        
    }
}

设圈前长度为x, 两指针在圈中相遇位置为k,圈周长为r。设相遇时ptr1跑了m圈,ptr2跑了n圈。因为ptr2速度是ptr1两倍,有:

2(x+k+mr) = x+k+nr

x+k = (n-m)r

因n和m均为整数且n>m,x+k应为整数圈。因为ptr1和ptr2在圈内位置为k,再跑x会到达圈起始点。从head跑x,同样会到达圈的起始点。所以讲ptr1移动到head,和ptr2以步长为1一起移动,相遇处就是圈的起始点。 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值