Skip to content

Commit b7a5017

Browse files
LinkedListCycle2.java added
1 parent d826286 commit b7a5017

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public ListNode detectCycle(ListNode head) {
3+
ListNode slow = head, fast = head;
4+
while(fast != null && fast.next != null) {
5+
fast = fast.next.next;
6+
slow = slow.next;
7+
if (slow == fast) {
8+
while (head != slow) {
9+
head = head.next;
10+
slow = slow.next;
11+
}
12+
return slow;
13+
}
14+
}
15+
return null;
16+
}
17+
}

0 commit comments

Comments
 (0)