forked from pujakri5438/Hacktoberfest-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotatelist.java
More file actions
110 lines (94 loc) · 3.42 KB
/
rotatelist.java
File metadata and controls
110 lines (94 loc) · 3.42 KB
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
public class RotateList {
//Represent a node of the doubly linked list
class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
int size = 0;
//Represent the head and tail of the doubly linked list
Node head, tail = null;
//addNode() will add a node to the list
public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);
//If list is empty
if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
//Size will count the number of nodes present in the list
size++;
}
//rotateList() will rotate the list by given n nodes
public void rotateList(int n) {
//Initially, current will point to head
Node current = head;
//n should not be 0 or greater than or equal to number of nodes present in the list
if(n == 0 || n >= size)
return;
else {
//Traverse through the list till current point to nth node
//after this loop, current will point to nth node
for(int i = 1; i < n; i++)
current = current.next;
//Now to move entire list from head to nth node and add it after tail
tail.next = head;
//Node next to nth node will be new head
head = current.next;
//Previous node to head should be null
head.previous = null;
//nth node will become new tail of the list
tail = current;
//tail's next will point to null
tail.next = null;
}
}
//display() will print out the nodes of the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing the pointer.
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
RotateList dList = new RotateList();
//Add nodes to the list
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);
System.out.println("Original List: ");
dList.display();
//Rotates list by 3 nodes
dList.rotateList(3);
System.out.println("Updated List: ");
dList.display();
}
}