forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
169 lines (136 loc) · 4.22 KB
/
LinkedList.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package vid24oops;
public class LinkedList {
Node head;
private int size;
LinkedList(){
this.size = 0;
}
class Node{
String data;
Node next;
Node(String data){
this.data = data;
this.next = null;
size++;
}
}
//Add to first position
public void addFirst(String data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
return;
}
size--;
newNode.next = head;
head = newNode;
}
//Add to last position
public void addLast(String data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
return;
}
size--;
Node currNode = head; //Head is considered as current node
while(currNode.next!=null) //Traverse till the last node
{
currNode = currNode.next; //The currentNode is pointing to next Node
}
currNode.next = newNode; //The new Node becomes the current node. Hence Node is successfully added at the end
}
//To print
public void printList(){
if(head == null){
System.out.println("List is empty");
return;
}
Node currNode = head;
while(currNode!=null)
{
System.out.print(currNode.data + "->");
currNode = currNode.next;
}
System.out.println("Null");
}
//To delete first
public void deleteFirst(){
if(head == null){
System.out.println("List is empty");
return;
}
head = head.next;
size--;
}
//To delete last
public void deleteLast(){
if(head == null){
System.out.println("List is empty");
return;
}
//Just incase we have only one node
if(head.next == null){
head = null;
return;
}
Node secondLast = head; //First it points to head(first element). But it traverse until last but one node
Node lastNode = head.next; //First it points to second element. But it traverse until for last node
while(lastNode.next!=null){
lastNode = lastNode.next;
secondLast = secondLast.next;
}
secondLast.next = null;
}
private int getSize(){
return size;
}
public void reverseList(){
if(head == null || head.next == null) //If no node or if one node
{
return;
}
Node prevNode = head;
Node currNode = head.next;
while(currNode!=null){
Node nextNode = currNode.next;
currNode.next = prevNode;
//Update
prevNode = currNode; //Ex: 6 7 8. Here 6 which is curr becomes prev
currNode= nextNode; // 7 becomes curr
}
//After reaching the last node
head.next = null;
head = prevNode; //Last node which we considered as prevNode becomes head
}
//Recursive way of reversing a linked list
public Node recursiveRev(Node head){
if(head == null || head.next == null){
return head;
}
Node newHead = recursiveRev(head.next);
head.next.next = head; //If 2 3 4, then 2 comes in head.next.next's position
head.next = null; // Connection btwn 2 -> 3 breaks. Its now from 3 -> 2
return newHead;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.addFirst("A");
ll.addFirst("is");
ll.printList();
ll.addLast("List");
ll.printList();
ll.addFirst("This");
ll.printList();
// ll.deleteFirst();
// ll.printList();
// ll.deleteLast();
// ll.printList();
System.out.println("Size of LL: " + ll.getSize());
System.out.println();
ll.reverseList();
ll.printList();
ll.head = ll.recursiveRev(ll.head);
ll.printList();
}
}