File tree Expand file tree Collapse file tree 2 files changed +123
-0
lines changed Expand file tree Collapse file tree 2 files changed +123
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+ import java .io .*;
3
+ import java .lang .*;
4
+ //Node of Linked list
5
+ class Node
6
+ {
7
+ int data ; //value of the node
8
+ Node next ; //next node
9
+ /*Constructor*/
10
+ Node (int x )
11
+ {
12
+ data = x ;
13
+ next = null ;
14
+ }
15
+ }
16
+ public class DetectLoop
17
+ { //function to create loop in linked list
18
+ public static void createLoop (Node head , Node tail , int k ){
19
+ //base case
20
+ if (x == 0 ) return ;
21
+ Node curr = head ;
22
+
23
+ for (int i =1 ; i <k ; i ++)
24
+ curr = curr .next ;// traversing the linked list till k
25
+ tail .next = curr ;
26
+ }
27
+ // Driver Code
28
+ public static void main (String [] args ){
29
+
30
+ Scanner sc = new Scanner (System .in );
31
+
32
+ //no. of nodes in linked list
33
+ int n = sc .nextInt ();
34
+
35
+ //head of Linked list
36
+ int num = sc .nextInt ();
37
+ Node head =new Node (num );
38
+
39
+ Node tail =head ;
40
+
41
+ //Linked List created
42
+ for (int i =0 ; i <n -1 ; i ++)
43
+ {
44
+ num = sc .nextInt ();
45
+ tail .next = new Node (num );
46
+ tail = tail .next ;
47
+ }
48
+
49
+ //Node to check if loop exists or not
50
+ int temp = sc .nextInt ();
51
+
52
+ //creating the loop
53
+ createLoop (head ,tail ,temp );
54
+
55
+ //creating object of x
56
+ Solution x = new Solution ();
57
+
58
+ //if loop exists
59
+ if (x .detectLoop (head ))
60
+ System .out .println ("Loop exists" );
61
+ else
62
+ System .out .println ("Loop doesn't exists" );
63
+ }
64
+ }
65
+
66
+ //detecting if the loop exists or not
67
+ class Solution {
68
+
69
+ public static boolean detectLoop (Node head ){
70
+ //base case
71
+ if (head ==null ||head .next ==null )
72
+ return false ;
73
+
74
+ Node slow = head ;
75
+ Node fast = head ;
76
+
77
+ while (fast != null && fast .next != null ) {
78
+
79
+ slow = slow .next ;
80
+ fast = fast .next .next ;
81
+
82
+ //if loop exists slow and fast pointer will surely intersect
83
+ if (slow ==fast )
84
+ return true ;
85
+ }
86
+
87
+ return false ;
88
+ }
89
+ }
Original file line number Diff line number Diff line change
1
+
2
+ # Sample Input:
3
+
4
+ 5
5
+
6
+ 1 6 3 7 8
7
+
8
+ 2
9
+
10
+
11
+
12
+ # Sample Output:
13
+
14
+ Loop exists
15
+
16
+
17
+
18
+ # Explaination:
19
+
20
+ 4--> size of linked list
21
+
22
+ 1-->6-->3-->7-->8 =>nodes of linked list
23
+
24
+ 2---> node to be checked
25
+
26
+ </br >
27
+
28
+ after creating the loop the detect loop fuction is called and the two pointer slow and fast meets if loop exists .
29
+ For better understanding,refer <a href =" https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190621160855/Detect-loop-in-a-linked-list.png " >here:
30
+
31
+
32
+ # Time complexity:
33
+
34
+ O(n)
You can’t perform that action at this time.
0 commit comments