Skip to content

Commit 913f0dd

Browse files
committed
Add cycle in a linked list
1 parent 73a4fc3 commit 913f0dd

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ Algorithms [Part 1](https://www.coursera.org/learn/algorithms-part1/) and [Part2
7171
| Reverse a linked list | [link](https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/560/) | ? | [Iterative](sites/leetcode/ReverseList.cpp) <br/> [Recursive](sites/leetcode/ReverseList.cpp)|
7272
| Merge two sorted | [link](https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/771/) | ? | [Simple](sites/leetcode/MergeSortedList.cpp) |
7373
| Palindrome List | [link](https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/772/) | ? | [Double Reverse](sites/leetcode/PalindromeList.cpp) |
74-
| | [link]()| ? | []() |
74+
| Cycle in Linked List | [link](https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/773/) | ? | [Slow and fast pointer](sites/leetcode/LinkedListCycle.cpp) |
75+
| | [link]() | ? | []() |
7576

7677
## [Google CodeJam](sites/googlecodejam/README.md)
7778
1. Google Code Jam 2018

sites/leetcode/LinkedListCycle.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/773/
2+
3+
#include <iostream>
4+
#include <vector>
5+
6+
class Node
7+
{
8+
public:
9+
int key;
10+
Node* next;
11+
12+
Node(int x) : key {x}, next{nullptr} {}
13+
};
14+
15+
class LinkedList
16+
{
17+
private:
18+
Node* head;
19+
20+
public:
21+
LinkedList() : head{nullptr} {}
22+
23+
void printList() {
24+
if (head == nullptr) {
25+
return;
26+
}
27+
28+
Node* temp = head;
29+
while (temp != nullptr) {
30+
std::cout << temp->key << " ";
31+
temp = temp->next;
32+
}
33+
std::cout << "\n";
34+
}
35+
36+
void printList(Node* node) {
37+
if (node == nullptr) {
38+
return;
39+
}
40+
41+
while (node != nullptr) {
42+
std::cout << node->key << " ";
43+
node = node->next;
44+
}
45+
std::cout << "\n";
46+
}
47+
48+
void addFront(int key) {
49+
Node* node = new Node(key);
50+
51+
if (head != nullptr) {
52+
node->next = head;
53+
}
54+
55+
head = node;
56+
}
57+
58+
void addKeys(const std::vector<int> keys) {
59+
for (auto key : keys) {
60+
addFront(key);
61+
}
62+
}
63+
64+
void createCycle() {
65+
if (head == nullptr || head->next == nullptr) {
66+
return;
67+
}
68+
69+
Node* second = head->next;
70+
Node* previous = nullptr;
71+
Node* current = head;
72+
73+
while (current != nullptr) {
74+
previous = current;
75+
current = current->next;
76+
}
77+
78+
previous->next = second;
79+
}
80+
81+
bool hasCycle() {
82+
if (head == nullptr) {
83+
return false;
84+
}
85+
86+
Node* slow = head;
87+
Node* fast = head->next;
88+
89+
while (fast != nullptr && fast->next != nullptr) {
90+
if (slow == fast) {
91+
return true;
92+
}
93+
94+
fast = fast->next->next;
95+
slow = slow->next;
96+
}
97+
98+
return false;
99+
}
100+
};
101+
102+
int main()
103+
{
104+
std::vector<int> keys {5, 1, 0, -3, -99};
105+
// std::vector<int> keys {5, 1};
106+
107+
LinkedList list;
108+
109+
std::cout << "List : ";
110+
list.addKeys(keys);
111+
list.printList();
112+
113+
list.createCycle();
114+
115+
std::cout << "List has cycle : " << std::boolalpha << list.hasCycle() << "\n";
116+
}

0 commit comments

Comments
 (0)