forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReversingCircularLinkedList.java
155 lines (129 loc) · 3.41 KB
/
ReversingCircularLinkedList.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Program to Reverse a Circular Linked List.
Algorithm explained:
-> Three pointers are used - previous, current and next.
-> previous points to null, current to start and next to start.address.
-> Changing current.address to point to the previous node (null).
-> previous now points to current.
-> current now points to next.
-> Iterating the previous steps until current points to start.
-> Adjust start.address to point to previous.
-> start now points to previous.
*/
import java.util.*;
class Node
{
public int data;
public Node address;
Node(int d)
{
data = d;
address = null;
}
}
public class ReversingCircularLinkedList
{
static Node start;
static int size;
//Default constructor to initialize the global variables.
ReversingCircularLinkedList()
{
start = null;
size = 0;
}
//This function is to reverse the circular linked list.
static Node reverse(Node start)
{
Node previous = null;
Node current = start;
Node next;
do
{
next = current.address;
current.address = previous;
previous = current;
current = next;
} while (current != (start));
(start).address = previous;
start = previous;
return start;
}
//This function is to create the circular linked list.
public void create(Node node,int i)
{
if (start == null)
{
start = node;
}
else
{
Node temp = start;
while (temp.address != null)
temp = temp.address;
temp.address = node;
if(i == size-1)
{
temp.address.address = start;
}
}
}
//This function is to display the circular linked list.
void display()
{
Node temp = start;
if(start == null)
{
System.out.println("List is empty");
return;
}
temp = start;
do
{
System.out.print(temp.data + " ");
temp = temp.address;
} while (temp != start);
System.out.println();
}
//This is the driver code.
public static void main()
{
Scanner scanner = new Scanner(System.in);
ReversingCircularLinkedList ob = new ReversingCircularLinkedList();
int data, i;
System.out.println("Enter size of Circular Linkedlist: ");
size = scanner.nextInt(); scanner.nextLine();
for(i = 0; i < size; i++)
{
System.out.println("Enter data in Circular Linkedlist: ");
data = scanner.nextInt(); scanner.nextLine();
ob.create(new Node(data), i);
}
System.out.println("Original Circular LinkedList: ");
ob.display();
start = reverse(start);
System.out.println("Reversed Circular LinkedList: ");
ob.display();
}
}
/*
Test Cases-
Enter size of Circular LinkedList:
5
Enter data in Circular LinkedList:
1
Enter data in Circular LinkedList:
2
Enter data in Circular LinkedList:
3
Enter data in Circular LinkedList:
4
Enter data in Circular LinkedList:
5
Original Circular LinkedList:
1 2 3 4 5
Reversed Circular LinkedList:
5 4 3 2 1
Time Complexity: O(n)
Space Complexity: O(n)
where n is the number of elements
*/