Length of loop in linked list
Write an efficient algorithm to, find the length of loop in given linked list.
Following steps are given to find the length of loops.
1) First need to detect loop of linked list. use two pointer first_ptr, and second_ptr. this both pointer start with first node of linked list. first_ptr are increment by one (visited next memory block) and second_ptr are increment by 2. If first_ptr are equal to second_ptr that means loop are exist in linked list.do step 2. Otherwise loop are not found.
2) In case loop are found then use one length integer variable and assign the value of 1. visit first_ptr to next node and continue this process until first_ptr not equal to second_ptr.
Result length of loop is : 6.
View nodes and pointers
Program for find length of loop in linked list . Time complexity O(n).
Execution Process Try it Yourself
- Singly linked list insert data in descending order
- Single linked list print node data at alternate positions
- Delete duplicate data on single linked list
- Single linked list delete middle element
- Swap alternate nodes of single linked list
- Move last node to front of linked list
- Detect loop in a given linked list