-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListCycle.java
47 lines (36 loc) · 1.11 KB
/
LinkedListCycle.java
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
47
/***
* Solution to https://leetcode.com/problems/linked-list-cycle/
**/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
class LinkedListCycle {
// basic idea is to have 2 pointers, one slow pointer and one fast pointer.
// slow pointer will on each iteration point to the next node, fast pointer will jump 2 places
// if there is a cycle in the list, the pointers will eventually point to the same node.
// if any or both of them are null, there is no cycle.
public boolean hasCycle(ListNode head) {
boolean hasCycle = false;
ListNode ptr1 = head;
ListNode ptr2 = head;
if (ptr1 == null)
return false;
while (ptr1.next != null && ptr2.next != null) {
ptr1 = ptr1.next;
ptr2 = ptr2.next.next;
if (ptr1 == null || ptr2 == null ) {
return false;
}
if (ptr1 == ptr2)
return true;
}
return hasCycle;
}
public static void main(String[] args) {
}
}