-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListCycleII.java
More file actions
46 lines (42 loc) · 1.08 KB
/
LinkedListCycleII.java
File metadata and controls
46 lines (42 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package implementation;
/**
* 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
* <p>
* 说明:不允许修改给定的链表。
* <p>
* 进阶:
* 你是否可以不用额外空间解决此题?
*/
//LinkedList definition
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class LinkedListCycleII {
public ListNode detectCycle(ListNode head) {
if (null == head || null == head.next) {
return null;
}
ListNode slow = head.next;
ListNode fast = head.next.next;
while (slow != fast) {
slow = slow.next;
if (null != fast && null != fast.next) {
fast = fast.next.next;
} else {
// no circle, return null
return null;
}
}
// find a circle, go to first node into circle
while (head != fast) {
head = head.next;
fast = fast.next;
}
return head;
}
}