Write a Java Program to Detect loop in a LinkedList

Write a Java Program to Detect loop in a LinkedList

 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
71
72
73
74
75
76
77
78
79
80
81
82
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.");
    }
  }
}

Final output

Leave a Comment