-
Notifications
You must be signed in to change notification settings - Fork 25
/
LinkedListCycleDetection.txt
47 lines (38 loc) · 1.4 KB
/
LinkedListCycleDetection.txt
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
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class LinkedListCycleDetection {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false; // No cycle if the list is empty or has only one node.
}
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // Move one step at a time.
fast = fast.next.next; // Move two steps at a time.
if (slow == fast) {
return true; // If there's a cycle, the two pointers will meet.
}
}
return false; // If we reach the end of the list, there's no cycle.
}
public static void main(String[] args) {
LinkedListCycleDetection cycleDetection = new LinkedListCycleDetection();
ListNode head = new ListNode(3);
ListNode node2 = new ListNode(2);
ListNode node0 = new ListNode(0);
ListNode node4 = new ListNode(4);
head.next = node2;
node2.next = node0;
node0.next = node4;
node4.next = node2; // Creating a cycle
boolean hasCycle = cycleDetection.hasCycle(head);
System.out.println("Does the linked list have a cycle? " + hasCycle);
}
}