-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD66_Detect Loop in linked list.cpp
More file actions
35 lines (30 loc) · 1.18 KB
/
D66_Detect Loop in linked list.cpp
File metadata and controls
35 lines (30 loc) · 1.18 KB
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
// You are given the head of a singly linked list. Your task is to determine if the linked list contains a loop. A loop exists in a linked list if the next pointer of the last node points to any other node in the list (including itself), rather than being null.
// Custom Input format:
// A head of a singly linked list and a pos (1-based index) which denotes the position of the node to which the last node points to. If pos = 0, it means the last node points to null, indicating there is no loop.
// Examples:
// Input: head: 1 -> 3 -> 4, pos = 2
// Output: true
// Explanation: There exists a loop as last node is connected back to the second node.
class Solution
{
public:
// Function to check if the linked list has a loop.
bool detectLoop(Node *head)
{
// your code here
if (head == nullptr)
return false;
Node *slow = head;
Node *fast = head;
while (fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
return true;
}
}
return false;
}
};