Leetcode - LinedListCycleII

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

[分析] 这题可以说是快慢指针的经典题。首先判断是否存在环,而后找寻环的入口点。自己的naive思路是从快慢指针相交点处将链表分割成两个新链表,两个新链表的第一个交点即为环的入口点。更优化的思路是参考来的方法2,比较巧妙,但自己总是记不住,明明也理解了……优化法解析参考[url]http://blog.youkuaiyun.com/kenden23/article/details/13871699[/url]


public class Solution {
// method 2
public ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null) {
fast = fast.next;
if (fast != null) {
slow = slow.next;
fast = fast.next;
if (slow == fast)
break;
} else {
return null;
}
}
if (fast == null)
return null;
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
// method 1
public ListNode detectCycle1(ListNode head) {
ListNode slow = head, fast = head;
// step1: check if there is cycle
while (fast != null) {
fast = fast.next;
if (fast != null) {
slow = slow.next;
fast = fast.next;
if (slow == fast)
break;
} else {
return null;
}
}
if (fast == null)
return null;

// step2: find the node which the cycle begins
// split list at the meet node of slow and fast & search the first same node of the two list
ListNode head2 = fast.next;
fast.next = null;
int len1 = 0, len2 = 0;
ListNode p = head, q = head2;
while (p != null) {
p = p.next;
len1++;
}
while (q != null) {
q = q.next;
len2++;
}
int diff = Math.abs(len1 - len2);
p = head;
q = head2;
if (len1 > len2) {
for (int i = 0; i < diff; i++)
p = p.next;
} else if (len1 < len2) {
for (int i = 0; i < diff; i++)
q = q.next;
}
while (p.val != q.val) {
p = p.next;
q = q.next;
}
fast.next = head2;
return p;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值