Problem
Detect a loop in a given linked list.
Due to some bug, a node may contain a link to some previous node in the linked list. This creates an loop which would lead to a program hang while traversing the list.
Give an algorithmic approach to detect a loop if it exits.

Solution
This is a standard interview question with a very simple solution.
Maintain two pointers, slow pointer which iterates over each node in the list & fast pointer which takes two steps at a time i.e skips every alternate node. If at any point of time slow & fast pointers intersect, then there is a loop in the list.

```#include<iostream>
using namespace std;

// list node structure
typedef struct list_node {
int data;
struct list_node *next;
}node;

// create new node
node *getNewNode(int data) {
node *new_node = new node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}

// append a node to the list
else {
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = getNewNode(data);
}
}

// create a list
node *createList() {
// create a loop for testing
while (ptr->next != NULL)
ptr++;
ptr->next = temp;
}

// traverse the list
while (ptr != NULL) {
cout<<ptr->data<<" ";
ptr = ptr->next;
}
}

// Detect a loop in the list
node *slow, *fast;
while (slow && fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
}

// main
int main() {