For linked list questions assume the following Linked list class definition:
struct Node {
int value;
Node* next;
Node(int num = 0) : value(num), next(nullptr) {}
};
class LinkedList {
Node* head;
public:
LinkedList() : head(nullptr) {}
// other member functions
};
Linked List Cycle
Write a member function of the class LinkedList that returns true if a cycle exists in the
linked list, false otherwise.
• Do not assume the existence of any other functions to use.
• Return false if the list is empty.
What does "cycle" mean? The singly linked list is a linear sequence. That is, starting with
the head, if we traverse each node, no node will be visited twice. However, if the next
pointer stored within a node points to an already traversed node, it will form a cycle (see
example below).
Example
For the list
head
18 7 15 27
The function should return true.