File tree 4 files changed +58
-6
lines changed
4 files changed +58
-6
lines changed Original file line number Diff line number Diff line change 1
1
.idea /
2
2
out /
3
+ # *.iml
4
+ # **/*.iml match all .iml file in all directory and sub-directory.
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <module type =" JAVA_MODULE" version =" 4" >
3
+ <component name =" NewModuleRootManager" inherit-compiler-output =" true" >
4
+ <exclude-output />
5
+ <content url =" file://$MODULE_DIR$" >
6
+ <sourceFolder url =" file://$MODULE_DIR$/src" isTestSource =" false" />
7
+ </content >
8
+ <orderEntry type =" inheritedJdk" />
9
+ <orderEntry type =" sourceFolder" forTests =" false" />
10
+ </component >
11
+ </module >
Original file line number Diff line number Diff line change 1
1
package com .cs .dsa .binarytrees ;
2
2
3
- import sun .misc .Queue ;
3
+ import java .util .LinkedList ;
4
+ import java .util .Queue ;
4
5
5
6
/*
6
7
* Created By Badri Paudel on 02/08/2022 - Software Developer at Threadcode Technologies
@@ -62,21 +63,21 @@ nodes are visited and moving up (or down)
62
63
63
64
protected void searchBFS () throws InterruptedException {
64
65
Node rNode = rootNode ; // access to the root node
65
- Queue queue = new Queue (); // queue to store visited nodes
66
+ Queue queue = new LinkedList (); // queue to store visited nodes
66
67
if (rNode != null ) {
67
- queue .enqueue (rNode ); // visited add to the queue
68
+ queue .add (rNode ); // visited add to the queue
68
69
// visit all by looping
69
70
System .out .print ("Visiting values ::: " );
70
71
while (!queue .isEmpty ()) {
71
- rNode = (Node ) queue .dequeue (); // pop that element off
72
+ rNode = (Node ) queue .poll (); // pop that element off
72
73
System .out .print (" " + rNode .element );
73
74
// VisIts left node and puts it into the queue
74
75
if (rNode .left != null ) {
75
- queue .enqueue (rNode .left );
76
+ queue .add (rNode .left );
76
77
}
77
78
// visits right node and put it in the queue
78
79
if (rNode .right != null ) {
79
- queue .enqueue (rNode .right );
80
+ queue .add (rNode .right );
80
81
}
81
82
}
82
83
System .out .println (" = Travelling BFS finished" );
Original file line number Diff line number Diff line change @@ -156,6 +156,40 @@ public Node removeNodeAtIndex(int index) {
156
156
return nodeToBeRemoved ;
157
157
}
158
158
159
+ // Reverse the LL: O(n), in-place
160
+ /**
161
+ * Swap head and tail and change the direction of the pointer (pointing to the next)
162
+ * Visualize by drawing a diagram, it should be clear.
163
+ * Main idea is to maintain pointer to prev, after and navigate accordingly until
164
+ * all the pointers(next) are flipped back.
165
+ */
166
+ public void reverse () {
167
+ if (length == 0 ) {
168
+ System .out .println ("LL is empty, so can't be reversed." );
169
+ return ;
170
+ }
171
+ if (length == 1 ) {
172
+ System .out .println ("There is only one node in the LL, already reversed." );
173
+ return ;
174
+ }
175
+ // swap the head and tail
176
+ Node temp = head ;
177
+ head = tail ;
178
+ tail = temp ;
179
+
180
+ Node prev = null ;
181
+ Node after ;
182
+
183
+ // change the direction of the pointer
184
+ for (int i =0 ; i < length ; i ++) {
185
+ after = temp .next ;
186
+ temp .next = prev ;
187
+ prev = temp ;
188
+ temp = after ;
189
+ }
190
+ System .out .println ("LL is now reversed." );
191
+ }
192
+
159
193
public Node getHead () {
160
194
System .out .println ("Currently head is pointing at node : " + head + " with value: " + head .value );
161
195
return this .head ;
@@ -205,5 +239,9 @@ public static void main(String[] args) {
205
239
206
240
linkedList .getHead ();
207
241
linkedList .getTail ();
242
+ linkedList .printList ();
243
+
244
+ linkedList .reverse ();
245
+ linkedList .printList ();
208
246
}
209
247
}
You can’t perform that action at this time.
0 commit comments