- Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathLinkedListCycle.java
More file actions
Latest commit
25 lines (25 loc) · 576 Bytes
/
Copy pathLinkedListCycle.java
File metadata and controls
25 lines (25 loc) · 576 Bytes
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
/**
* Given a linked list, determine if it has a cycle in it.
*
* <p>Follow up: Can you solve it without using extra space?
*/
publicclassLinkedListCycle {
publicbooleanhasCycle(ListNodehead) {
if (head == null) returnfalse;
ListNodefast = head;
ListNodelate = head;
do {
fast = fast.next;
late = late.next;
if (late == null) {
returnfalse;
} else {
late = late.next;
if (fast == late) {
returntrue;
}
}
} while (fast != null && late != null);
returnfalse;
}
}