LeetCode-141.142. Linked List Cycle (JAVA)链表找环
来源:互联网 发布:mac wine下载安装 编辑:程序博客网 时间:2024/06/01 09:24
141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
链表是否存在环
Set解法
public boolean hasCycle(ListNode head) {Set<ListNode> set = new HashSet<>();while (head != null) {if (!set.add(head))return true;head = head.next;}return false;}
快慢指针:
public boolean hasCycle(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if(fast==slow)return true;}return false;}
142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull
.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
Set解法
public ListNode detectCycle(ListNode head) {Set<ListNode> set = new HashSet<>();while (head != null) {if (!set.add(head))return head;head = head.next;}return null;}
快慢指针
感谢:http://www.jianshu.com/p/ce7f035daf74 解析
图中,
X
是链表的起点。Y
是环的起点。Z
是fast
和slow
首次相遇的地方(二者同时从X出发,slow
每次移动一步,fast
每次移动两步)。a
,b
,c
分别表示XY
(蓝色),YZ
(红色),ZY
(绿色)的长度。
当fast
和slow
在Z
点首次相遇时:
fast
移动的距离是:a + b + c + bslow
移动的距离是:a + b
因为fast
的移动速度是slow
的两倍,所以:
(a + b + c + d) == 2 * (a + b)
由此可以推出:
a == c
我们需要用上面的推论来寻找环的起点(Y
)。
当fast
和slow
首次相遇时,我们就到了Z
点。
由于a == c
,也就是X
到Y
与 Z
到Y
的距离相等。
因此,如果我们让指针p
和q
分别从X
和Z
出发,并且每次都移动一步,当它们相遇时,恰好就是环的起点Y
。
public ListNode detectCycle(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {fast = head;while (fast != slow) {fast = fast.next;slow = slow.next;}return slow;}}return null;}
0 0
- LeetCode-141.142. Linked List Cycle (JAVA)链表找环
- [Leetcode] Linked List Cycle (Java)
- [LeetCode][Java] Linked List Cycle
- leetcode-Java-141. Linked List Cycle.java
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- [LeetCode]Linked List Cycle
- LeetCode | Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode: Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode] - Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode]Linked List Cycle
- ubuntu16.04安装opencv3.2
- 数论
- LintCode 53 翻转字符串
- ES6学习及参考资料
- VR develop resource
- LeetCode-141.142. Linked List Cycle (JAVA)链表找环
- C、Delphi和PHP的基本语法对照表
- Ancient Cipher
- 什么是HTTPS?
- CentOS7 Minimal 最小化安装后再安装GNOME图形界面
- 大数据处理问题
- 设计模式之适配器模式
- eclipse配置tomcat、发布项目遇到问题解决方法
- Qt如何设置界面透明