C Program For Detecting Loop In A Linked List Last Updated : 03 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. Solution: Floyd's Cycle-Finding Algorithm Approach: This is the fastest method and has been described below: Traverse linked list using two pointers.Move one pointer(slow_p) by one and another pointer(fast_p) by two.If these pointers meet at the same node then there is a loop. If pointers do not meet then linked list doesn't have a loop. The below image shows how the detectloop function works in the code: Implementation of Floyd's Cycle-Finding Algorithm: C // C program to detect loop in a linked list #include <stdio.h> #include <stdlib.h> /* Link list node */ struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { /* allocate node */ struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); /* put in the data */ new_node->data = new_data; /* link the old list of the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } int detectLoop(struct Node* list) { struct Node *slow_p = list, *fast_p = list; while (slow_p && fast_p && fast_p->next) { slow_p = slow_p->next; fast_p = fast_p->next->next; if (slow_p == fast_p) { return 1; } } return 0; } /* Driver program to test above function*/ int main() { /* Start with the empty list */ struct Node* head = NULL; push(&head, 20); push(&head, 4); push(&head, 15); push(&head, 10); /* Create a loop for testing */ head->next->next->next->next = head; if (detectLoop(head)) printf("Loop found"); else printf("No Loop"); return 0; } OutputLoop found Complexity Analysis: Time complexity: O(n). Only one traversal of the loop is needed.Auxiliary Space:O(1). There is no space required. How does above algorithm work? Please See : How does Floyd’s slow and fast pointers approach work?https://www.youtube.com/watch?v=Aup0kOWoMVg Please refer complete article on Detect loop in a linked list for more details! Comment More infoAdvertise with us Next Article Detect and Remove Loop in Linked List K kartik Follow Improve Article Tags : C Language Linked Lists loop Amazon Samsung Accolite MAQ Software Tortoise-Hare-Approach +4 More Practice Tags : AccoliteAmazonMAQ SoftwareSamsung Similar Reads Detect Loop or Cycle in Linked List Given a singly linked list, check if the linked list has a loop (cycle) or not. A loop means that the last node of the linked list is connected back to a node in the same list.Examples:Input: head: 1 -> 3 -> 4 -> 3Output: true Input: head: 1 -> 8 -> 3 -> 4 -> NULL Output: false 10 min read Detect and Remove Loop in Linked List Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to remove the loop from the linked list (if it exists).Example:Input: Output: 1 -> 3 -> 4 Explanation: The Loop is removed from 15 min read Detect Cycle in a Linked List using Map Given a Linked List, check if the linked list has a loop or not.There are various methods shown here: Detect Cycle in Linked ListExample Input: 20->4->54->6->NULL Output: No loop is detected. Explanation: While traversing the linked list, we reach the end of the linked list. Therefore, n 7 min read Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the 8 min read C Program for Reverse a linked list Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL I 4 min read Program for Nth node from the end of a Linked List Given a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1.Examples:Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list.Input: 35 -> 14 min read Like