Algorithm


  1. Initialize two pointers, slow and fast, pointing to the head of the linked list.
  2. Traverse the linked list using a loop until either the fast pointer or the next of the fast pointer becomes null.
  3. In each iteration, move the slow pointer one step and the fast pointer two steps.
  4. If at any point the slow pointer and fast pointer meet (i.e., they point to the same node), it indicates the presence of a loop in the linked list.
  5. If the fast pointer reaches the end of the linked list (i.e., becomes null), it means there is no loop in the linked list.

 

Code Examples

#1 Code Example- Detect Loop in a LinkedList

Code - Java Programming

class LinkedList {

  // create an object of Node class
  // represent the head of the linked list
  Node head;

  // static inner class
  static class Node {
    int value;

    // connect each node to next node
    Node next;

    Node(int d) {
      value = d;
      next = null;
    }
  }

  // check if loop is present
  public boolean checkLoop() {

    // create two references at start of LinkedList
    Node first = head;
    Node second = head;

    while(first != null && first.next !=null) {

      // move first reference by 2 nodes
      first = first.next.next;

      // move second reference by 1 node
      second = second.next;

      // if two references meet
      // then there is a loop
      if(first == second) {
        return true;
      }
    }

    return false;
  }

  public static void main(String[] args) {

    // create an object of LinkedList
    LinkedList linkedList = new LinkedList();

    // assign values to each linked list node
    linkedList.head = new Node(1);
    Node second = new Node(2);
    Node third = new Node(3);
    Node fourth = new Node(4);

    // connect each node of linked list to next node
    linkedList.head.next = second;
    second.next = third;
    third.next = fourth;

    // make loop in LinkedList
    fourth.next = second;

    // printing node-value
    System.out.print("LinkedList: ");
    int i = 1;
    while (i <= 4) {
      System.out.print(linkedList.head.value + " ");
      linkedList.head = linkedList.head.next;
      i++;
    }

    // call method to check loop
    boolean loop = linkedList.checkLoop();
    if(loop) {
      System.out.println("\nThere is a loop in LinkedList.");
    }
    else {
      System.out.println("\nThere is no loop in LinkedList.">;
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
LinkedList: 1 2 3 4
There is a loop in LinkedList.
Advertisements

Demonstration


Java Programing Example to Detect loop in a LinkedList-DevsEnv