Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
[分析] 这题可以说是快慢指针的经典题。首先判断是否存在环,而后找寻环的入口点。自己的naive思路是从快慢指针相交点处将链表分割成两个新链表,两个新链表的第一个交点即为环的入口点。更优化的思路是参考来的方法2,比较巧妙,但自己总是记不住,明明也理解了……优化法解析参考
http://blog.csdn.net/kenden23/article/details/13871699
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;
}
}
分享到:
相关推荐
《leetcode-solutions》,刷算法题,需要有一定的英文阅读能力。。。
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
IDEA 插件,lettcode刷题,leetcode-editor7.4版本下载进行本地导入(直接将压缩包拖进IDEA即可)
Algorithm-leetcode-spider.zip,leetcode公司,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
leetcode 答案解析 golang解答
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
然后进入到LeetCode-Spider目录中修改config.json,其中outputDir需要填写该工程的/docs/views文件夹路径 { "username": "aaa", "password": "bbb", "outputDir": "/Users/liuyao/Downloads/LeetCode-Blog-Test/docs...
970. 强整数对数运算function powerfulIntegers(x: number, y: number, bound: number): numb
leetcode-helper-1.7.1
leetcode-tag-dynamic programming
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
leetcode-tag-Tree
leetcode-tag-Stack
leetcode-tag-array
Algorithm-LeetCode-Solution-From-GuaZiDou.zip,Leetcode解决方案Gitbook,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...