Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18edf00

Browse files
Add fast and slow pointers code
1 parent c43d923 commit 18edf00

File tree

7 files changed

+654
-0
lines changed

7 files changed

+654
-0
lines changed

‎patterns/c#/FastAndSlowPointers.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class ListNode {
5+
public int val;
6+
public ListNode next;
7+
public ListNode(int x) {
8+
val = x;
9+
next = null;
10+
}
11+
}
12+
13+
public class FastAndSlowPointers {
14+
// LeetCode 141 - Linked List Cycle (HashSet Approach)
15+
public bool HasCycleHashSetApproach(ListNode head) {
16+
HashSet<ListNode> visited = new HashSet<ListNode>();
17+
ListNode current = head;
18+
while (current != null) {
19+
if (visited.Contains(current)) {
20+
return true; // Cycle detected
21+
}
22+
visited.Add(current);
23+
current = current.next;
24+
}
25+
return false; // No cycle
26+
}
27+
28+
// LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach)
29+
public bool HasCycleFastAndSlowPointersApproach(ListNode head) {
30+
if (head == null || head.next == null) return false;
31+
ListNode slow = head, fast = head;
32+
while (fast != null && fast.next != null) {
33+
slow = slow.next;
34+
fast = fast.next.next;
35+
if (slow == fast) return true;
36+
}
37+
return false;
38+
}
39+
40+
// LeetCode 876 - Middle of the Linked List (Counting Approach)
41+
public ListNode MiddleNodeCountingApproach(ListNode head) {
42+
int count = 0;
43+
ListNode current = head;
44+
while (current != null) {
45+
count++;
46+
current = current.next;
47+
}
48+
current = head;
49+
for (int i = 0; i < count / 2; i++) {
50+
current = current.next;
51+
}
52+
return current;
53+
}
54+
55+
// LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach)
56+
public ListNode MiddleNodeFastAndSlowPointerApproach(ListNode head) {
57+
ListNode slow = head, fast = head;
58+
while (fast != null && fast.next != null) {
59+
slow = slow.next;
60+
fast = fast.next.next;
61+
}
62+
return slow;
63+
}
64+
65+
// LeetCode 202 - Happy Number (HashSet Approach)
66+
private int GetSumOfSquares(int n) {
67+
int sum = 0;
68+
while (n > 0) {
69+
int digit = n % 10;
70+
sum += digit * digit;
71+
n /= 10;
72+
}
73+
return sum;
74+
}
75+
76+
public bool IsHappyHashSetApproach(int n) {
77+
HashSet<int> seen = new HashSet<int>();
78+
while (n != 1 && !seen.Contains(n)) {
79+
seen.Add(n);
80+
n = GetSumOfSquares(n);
81+
}
82+
return n == 1;
83+
}
84+
85+
// LeetCode 202 - Happy Number (Fast and Slow Pointer Approach)
86+
public bool IsHappyFastAndSlowPointersApproach(int n) {
87+
int slow = n;
88+
int fast = GetSumOfSquares(n);
89+
while (fast != 1 && slow != fast) {
90+
slow = GetSumOfSquares(slow);
91+
fast = GetSumOfSquares(GetSumOfSquares(fast));
92+
}
93+
return fast == 1;
94+
}
95+
}

‎patterns/c++/FastAndSlowPointers.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <unordered_set>
2+
using namespace std;
3+
4+
class ListNode {
5+
public:
6+
int val;
7+
ListNode* next;
8+
ListNode(int x) : val(x), next(nullptr) {}
9+
};
10+
11+
class FastAndSlowPointers {
12+
public:
13+
// LeetCode 141 - Linked List Cycle (HashSet Approach)
14+
bool hasCycleHashSetApproach(ListNode* head) {
15+
unordered_set<ListNode*> visited;
16+
ListNode* current = head;
17+
while (current != nullptr) {
18+
if (visited.find(current) != visited.end()) {
19+
return true; // Cycle detected
20+
}
21+
visited.insert(current);
22+
current = current->next;
23+
}
24+
return false; // No cycle
25+
}
26+
27+
// LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach)
28+
bool hasCycleFastAndSlowPointersApproach(ListNode* head) {
29+
if (head == nullptr || head->next == nullptr) {
30+
return false;
31+
}
32+
ListNode* slow = head;
33+
ListNode* fast = head;
34+
while (fast != nullptr && fast->next != nullptr) {
35+
slow = slow->next;
36+
fast = fast->next->next;
37+
if (slow == fast) {
38+
return true; // Cycle detected
39+
}
40+
}
41+
return false; // No cycle
42+
}
43+
44+
// LeetCode 876 - Middle of the Linked List (Counting Approach)
45+
ListNode* middleNodeCountingApproach(ListNode* head) {
46+
int count = 0;
47+
ListNode* current = head;
48+
while (current != nullptr) {
49+
count++;
50+
current = current->next;
51+
}
52+
current = head;
53+
for (int i = 0; i < count / 2; i++) {
54+
current = current->next;
55+
}
56+
return current;
57+
}
58+
59+
// LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach)
60+
ListNode* middleNodeFastAndSlowPointerApproach(ListNode* head) {
61+
ListNode* slow = head;
62+
ListNode* fast = head;
63+
while (fast != nullptr && fast->next != nullptr) {
64+
slow = slow->next;
65+
fast = fast->next->next;
66+
}
67+
return slow;
68+
}
69+
70+
// LeetCode 202 - Happy Number (HashSet Approach)
71+
int getSumOfSquares(int n) {
72+
int sum = 0;
73+
while (n > 0) {
74+
int digit = n % 10;
75+
sum += digit * digit;
76+
n /= 10;
77+
}
78+
return sum;
79+
}
80+
81+
bool isHappyHashSetApproach(int n) {
82+
unordered_set<int> seen;
83+
while (n != 1 && seen.find(n) == seen.end()) {
84+
seen.insert(n);
85+
n = getSumOfSquares(n);
86+
}
87+
return n == 1;
88+
}
89+
90+
// LeetCode 202 - Happy Number (Fast and Slow Pointer Approach)
91+
bool isHappyFastAndSlowPointersApproach(int n) {
92+
int slow = n;
93+
int fast = getSumOfSquares(n);
94+
while (fast != 1 && slow != fast) {
95+
slow = getSumOfSquares(slow);
96+
fast = getSumOfSquares(getSumOfSquares(fast));
97+
}
98+
return fast == 1;
99+
}
100+
};

‎patterns/go/fast_and_slow_pointers.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type ListNode struct {
6+
Val int
7+
Next *ListNode
8+
}
9+
10+
// LeetCode 141 - Linked List Cycle (HashSet Approach)
11+
func hasCycleHashSetApproach(head *ListNode) bool {
12+
visited := map[*ListNode]bool{}
13+
current := head
14+
for current != nil {
15+
if visited[current] {
16+
return true
17+
}
18+
visited[current] = true
19+
current = current.Next
20+
}
21+
return false
22+
}
23+
24+
// LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach)
25+
func hasCycleFastAndSlowPointersApproach(head *ListNode) bool {
26+
if head == nil || head.Next == nil {
27+
return false
28+
}
29+
slow, fast := head, head
30+
for fast != nil && fast.Next != nil {
31+
slow = slow.Next
32+
fast = fast.Next.Next
33+
if slow == fast {
34+
return true
35+
}
36+
}
37+
return false
38+
}
39+
40+
// LeetCode 876 - Middle of the Linked List (Counting Approach)
41+
func middleNodeCountingApproach(head *ListNode) *ListNode {
42+
count := 0
43+
current := head
44+
for current != nil {
45+
count++
46+
current = current.Next
47+
}
48+
current = head
49+
for i := 0; i < count/2; i++ {
50+
current = current.Next
51+
}
52+
return current
53+
}
54+
55+
// LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach)
56+
func middleNodeFastAndSlowPointerApproach(head *ListNode) *ListNode {
57+
slow, fast := head, head
58+
for fast != nil && fast.Next != nil {
59+
slow = slow.Next
60+
fast = fast.Next.Next
61+
}
62+
return slow
63+
}
64+
65+
// LeetCode 202 - Happy Number (HashSet Approach)
66+
func getSumOfSquares(n int) int {
67+
sum := 0
68+
for n > 0 {
69+
digit := n % 10
70+
sum += digit * digit
71+
n /= 10
72+
}
73+
return sum
74+
}
75+
76+
func isHappyHashSetApproach(n int) bool {
77+
seen := map[int]bool{}
78+
for n != 1 && !seen[n] {
79+
seen[n] = true
80+
n = getSumOfSquares(n)
81+
}
82+
return n == 1
83+
}
84+
85+
// LeetCode 202 - Happy Number (Fast and Slow Pointer Approach)
86+
func isHappyFastAndSlowPointersApproach(n int) bool {
87+
slow := n
88+
fast := getSumOfSquares(n)
89+
for fast != 1 && slow != fast {
90+
slow = getSumOfSquares(slow)
91+
fast = getSumOfSquares(getSumOfSquares(fast))
92+
}
93+
return fast == 1
94+
}
95+
96+
func main() {
97+
// You can test the implementations here
98+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /