-
Notifications
You must be signed in to change notification settings - Fork 618
/
Cycle_Detection_In_Linked_List.java
131 lines (118 loc) · 4.05 KB
/
Cycle_Detection_In_Linked_List.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Floyd’s Cycle-Finding Algorithm
Approach: This is the fastest method and optimized approach to traverse a LinkedList:
* Traverse linked list using two pointers.
* Move one pointer(slow_p) by one and another pointer(fast_p) by two.
* If these pointers meet at the same node then there is a loop. If pointers do not meet then linked list doesn’t have a loop.
*/
import java.io.*;
import java.util.*;
public class Cycle_Detection_In_Linked_List {
// Creation of SinglyLinkedListNode
static class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
// Creation of Singly Linked List with insertNode function
static class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedListNode tail;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
}
public void insertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
static boolean hasCycle(SinglyLinkedListNode head) {
if(head == null ){
return false;
}
SinglyLinkedListNode slow = head;
SinglyLinkedListNode fast = head.next;
while(slow != fast) {
if(fast == null || fast.next == null || slow == null ) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
System.out.println("Enter the test cases: ");
int tests = scanner.nextInt();
for (int testsItr = 0; testsItr < tests; testsItr++) {
System.out.println("Enter the pos ( is used to denote the index of the node that tail's next pointer is connected to ): ");
int index = scanner.nextInt();
SinglyLinkedList llist = new SinglyLinkedList();
System.out.println("Enter the llist count: ");
int llistCount = scanner.nextInt();
for (int i = 0; i < llistCount; i++) {
System.out.println("Enter next node: ");
int llistItem = scanner.nextInt();
llist.insertNode(llistItem);
}
SinglyLinkedListNode extra = new SinglyLinkedListNode(-1);
SinglyLinkedListNode temp = llist.head;
for (int i = 0; i < llistCount; i++) {
if (i == index) {
extra = temp;
}
if (i != llistCount-1) {
temp = temp.next;
}
}
temp.next = extra;
boolean result = hasCycle(llist.head);
if( result == true ) {
System.out.println("The given LList has cycle. ");
}else {
System.out.println("The LList has no cycle ");
}
}
scanner.close();
}
}
/*
Sample Input 1:
Enter the test cases:
1
Enter the pos ( is used to denote the index of the node that tail's next pointer is connected to ):
1
Enter the llist count:
3
Enter next node:
1
Enter next node:
2
Enter next node:
3
The given LList has cycle.
Sample Input 2:
Enter the test cases:
1
Enter the pos ( is used to denote the index of the node that tail's next pointer is connected to ):
-1
Enter the llist count:
1
Enter next node:
1
The LList has no cycle
Complexity Analysis:
* Time complexity: O(N)
Only one traversal of the loop is needed.
* Auxiliary Space: O(1)
There is no space required.
*/