Table of Contents
If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.
One of the most popular interview question nowadays is “How to detect loop/cycle in LinkedList”. So I thought I should cover this question. This question is more related to data structure. You can also find start node of loop if loop exists.
Java Linked List Interview Programs:
- How to reverse a linked list in java
- How to reverse a linked list in pairs
- How to find middle element of linked list in java
- How to detect a loop in linked list in java
- Find start node of loop in linkedlist
- How to find nth element from end of linked list
- How to check if linked list is palindrome in java
- Add two numbers represented by linked list in java
First approach that you may think may something look like:
- Traverse through each node till end , tracking visited node using visited flag.
- If you find node that is already visited, then there is a loop in LinkedList and if you reach till end while traversing then there is no loop in LinkedList
But problem with above approach is, in most cases you can not change data structure of LinkedList node, so you won’t be able to add visited flag to it.
Efficient approach:
- Use two pointer fastPtr and slowPtr and initialize both to head of linkedlist
- Move fastPtr by two nodes and slowPtr by one node in each iteration.
- If fastPtr and slowPtr meet at some iteration , then there is a loop in linkedlist.
- If fastPtr reaches to the end of linkedlist without meeting slow pointer then there is no loop in linkedlist (i.e fastPtr->next or fastPtr->next->next become null)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public boolean ifLoopExists() { Node fastPtr = head; Node slowPtr = head; while (fastPtr != null && fastPtr.next != null) { fastPtr = fastPtr.next.next; slowPtr = slowPtr.next; if (slowPtr == fastPtr) return true; } return false; } |
Lets create linked list without loop :
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package org.arpit.java2blog; public class LinkedList{ private Node head; private static class Node { private int value; private Node next; Node(int value) { this.value = value; } } public void addToTheLast(Node node) { if (head == null) { head = node; } else { Node temp = head; while (temp.next != null) temp = temp.next; temp.next = node; } } public void printList() { Node temp = head; while (temp != null) { System.out.format("%d ", temp.value); temp = temp.next; } System.out.println(); } public boolean ifLoopExists() { Node fastPtr = head; Node slowPtr = head; while (fastPtr != null && fastPtr.next != null) { fastPtr = fastPtr.next.next; slowPtr = slowPtr.next; if (slowPtr == fastPtr) return true; } return false; } public static void main(String[] args) { LinkedList list = new LinkedList(); // Creating a linked list list.addToTheLast(new Node(5)); list.addToTheLast(new Node(6)); list.addToTheLast(new Node(7)); list.addToTheLast(new Node(1)); list.addToTheLast(new Node(2)); list.printList(); // Test if loop existed or not System.out.println("Loop existed-->" + list.ifLoopExists()); } } |
Run above program, you will get following output:
1 2 3 4 |
5 6 7 1 2 Loop existed-->false |
Lets create a loop in linkedlist
Now we will connect last node to the nodewith value 7 and it will create loop in linked list, so change above main method to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static void main(String[] args) { LinkedList list = new LinkedList(); // Creating a linked list Node loopNode=new Node(7); list.addToTheLast(new Node(5)); list.addToTheLast(new Node(6)); list.addToTheLast(loopNode); list.addToTheLast(new Node(1)); list.addToTheLast(new Node(2)); list.printList(); // creating a loop list.addToTheLast(loopNode); // Test if loop existed or not System.out.println("Loop existed-->" + list.ifLoopExists()); } |
Logically our linkedlist with loop can be represented as :
Run above program, you will get following output:
1 2 3 4 |
5 6 7 1 2 Loop existed-->true |
Please go through java interview programs for more such programs.
addToLast will not add nodes. Plz check it
It is added
This will looks like wrong for couple of scenarios
This will work for Odd loops , If the loop exist like this?
5->6->7>8>1>5>6