Skip to content

Commit f4132a8

Browse files
authored
Merge pull request ByteByteGoHq#18 from ongshunping/cpp-solutions-fast-and-slow-pointers
Add C++ solutions for Chapter 4 (Fast and Slow Pointers)
2 parents 77cb109 + 1fce15c commit f4132a8

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
bool happyNumber(int n) {
2+
int slow = n;
3+
int fast = n;
4+
while (true) {
5+
slow = getNextNum(slow);
6+
fast = getNextNum(getNextNum(fast));
7+
if (fast == 1) {
8+
return true;
9+
}
10+
// If the fast and slow pointers meet, a cycle is detected.
11+
// Hence, 'n' is not a happy number.
12+
else if (fast == slow) {
13+
return false;
14+
}
15+
}
16+
}
17+
18+
int getNextNum(int x) {
19+
int nextNum = 0;
20+
while (x > 0) {
21+
// Extract the last digit of 'x'.
22+
int digit = x % 10;
23+
// Truncate (remove) the last digit from 'x' using integer
24+
// division.
25+
x /= 10;
26+
// Add the square of the extracted digit to the sum.
27+
nextNum += digit * digit;
28+
}
29+
return nextNum;
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "ds/ListNode.h"
2+
using ds::ListNode;
3+
4+
/**
5+
* Definition of ListNode:
6+
* struct ListNode {
7+
* int val;
8+
* ListNode* next;
9+
* ListNode(int val = 0, ListNode* next = nullptr) : val(val), next(next) {}
10+
* };
11+
*/
12+
13+
bool linkedListLoop(ListNode* head) {
14+
ListNode* slow = head;
15+
ListNode* fast = head;
16+
// Check both 'fast' and 'fast->next' to avoid null pointer
17+
// exceptions when we perform 'fast->next' and 'fast->next->next'.
18+
while (fast != nullptr && fast->next != nullptr) {
19+
slow = slow->next;
20+
fast = fast->next->next;
21+
if (fast == slow) {
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <unordered_set>
2+
#include "ds/ListNode.h"
3+
using ds::ListNode;
4+
5+
/**
6+
* Definition of ListNode:
7+
* struct ListNode {
8+
* int val;
9+
* ListNode* next;
10+
* ListNode(int val = 0, ListNode* next = nullptr) : val(val), next(next) {}
11+
* };
12+
*/
13+
14+
bool linkedListLoopNaive(ListNode* head) {
15+
std::unordered_set<ListNode*> visited;
16+
ListNode* curr = head;
17+
while (curr) {
18+
// Cycle detected if the current node has already been visited.
19+
if (visited.find(curr) != visited.end()) {
20+
return true;
21+
}
22+
visited.insert(curr);
23+
curr = curr->next;
24+
}
25+
return false;
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "ds/ListNode.h"
2+
using ds::ListNode;
3+
4+
/**
5+
* Definition of ListNode:
6+
* struct ListNode {
7+
* int val;
8+
* ListNode* next;
9+
* ListNode(int val = 0, ListNode* next = nullptr) : val(val), next(next) {}
10+
* };
11+
*/
12+
13+
ListNode* linkedListMidpoint(ListNode* head) {
14+
ListNode* slow = head;
15+
ListNode* fast = head;
16+
// When the fast pointer reaches the end of the list, the slow
17+
// pointer will be at the midpoint of the linked list.
18+
while (fast != nullptr && fast->next != nullptr) {
19+
slow = slow->next;
20+
fast = fast->next->next;
21+
}
22+
return slow;
23+
}

0 commit comments

Comments
 (0)