-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathFastAndSlowPointers.java
123 lines (99 loc) · 3.26 KB
/
FastAndSlowPointers.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package patterns.java;
import java.util.HashSet;
public class FastAndSlowPointers {
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/*
* ********** LeetCode 141 - Linked List Cycle (https://leetcode.com/problems/linked-list-cycle/) **********
*/
public boolean hasCycleHashSetAppraoch(ListNode head) {
HashSet<ListNode> visited = new HashSet<>();
ListNode current = head;
while (current != null) {
if (visited.contains(current)) {
return true; // Cycle detected
}
visited.add(current);
current = current.next;
}
return false; // No cycle
}
public boolean hasCycleFastAndSlowPointersAppraoch(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 slow pointer by 1 step
fast = fast.next.next; // Move fast pointer by 2 steps
if (slow == fast) {
return true; // Cycle detected
}
}
return false; // No cycle
}
/*
* ********** LeetCode 876 - Middle of the Linked List (https://leetcode.com/problems/middle-of-the-linked-list/description/) **********
*/
public ListNode middleNodeCountingApproach(ListNode head) {
int count = 0;
ListNode current = head;
// First pass to count the number of nodes
while (current != null) {
count++;
current = current.next;
}
// Second pass to find the middle node
current = head;
for (int i = 0; i < count / 2; i++) {
current = current.next;
}
return current; // This will be the middle node
}
public ListNode middleNodeFastAndSlowPointerApproach(ListNode head) {
ListNode slow = head;
ListNode fast = head;
// Move slow by 1 and fast by 2 steps
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow; // Slow will be at the middle node
}
/*
* ********** LeetCode 202 - Happy Number (https://leetcode.com/problems/happy-number/description/) **********
*/
private int getSumOfSquares(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
public boolean isHappyHashSetApproach(int n) {
HashSet<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getSumOfSquares(n);
}
return n == 1;
}
public boolean isHappyFastAndSlowPointersApproach(int n) {
int slow = n;
int fast = getSumOfSquares(n);
while (fast != 1 && slow != fast) {
slow = getSumOfSquares(slow); // Move slow by 1 step
fast = getSumOfSquares(getSumOfSquares(fast)); // Move fast by 2 steps
}
return fast == 1;
}
}