/
LinkedListCycle.java
42 lines (40 loc) · 1.24 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
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
// https://leetcode.com/problems/linked-list-cycle
public class Solution {
// My solution: If there is a loop, the count will be greater than the max possible length of the list.
// This is not a good solution. See below for a better solution.
public boolean hasCycleInEff(ListNode head) {
int count = 0;
while (head != null) {
head = head.next;
if (++count > 10000) return true;
}
return false;
}
// We are incrementing slow by one and fast by two.
// If linked list doesn't have cycle, the fast will reach null and return false.
// But if at some point fast equals slow, this means it does have a cycle and return true.
public boolean hasCycle(ListNode head) {
if(head == null) return false;
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
return true;
}
return false;
}
}