Skip to content

Commit 00f09fa

Browse files
author
Badri Paudel
committed
LinkedList reverse method added, Queue changed to java.util in binary tree
1 parent 02713c9 commit 00f09fa

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea/
22
out/
3+
#*.iml
4+
#**/*.iml match all .iml file in all directory and sub-directory.

data_structure_algorithm.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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>

src/com/cs/dsa/binarytrees/BinaryTree.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cs.dsa.binarytrees;
22

3-
import sun.misc.Queue;
3+
import java.util.LinkedList;
4+
import java.util.Queue;
45

56
/*
67
* 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)
6263

6364
protected void searchBFS() throws InterruptedException {
6465
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
6667
if (rNode != null) {
67-
queue.enqueue(rNode); // visited add to the queue
68+
queue.add(rNode); // visited add to the queue
6869
// visit all by looping
6970
System.out.print("Visiting values ::: ");
7071
while (!queue.isEmpty()) {
71-
rNode = (Node) queue.dequeue(); // pop that element off
72+
rNode = (Node) queue.poll(); // pop that element off
7273
System.out.print(" " + rNode.element);
7374
// VisIts left node and puts it into the queue
7475
if (rNode.left != null) {
75-
queue.enqueue(rNode.left);
76+
queue.add(rNode.left);
7677
}
7778
// visits right node and put it in the queue
7879
if (rNode.right != null) {
79-
queue.enqueue(rNode.right);
80+
queue.add(rNode.right);
8081
}
8182
}
8283
System.out.println(" = Travelling BFS finished");

src/com/cs/dsa/linkedlist/LinkedList.java

+38
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,40 @@ public Node removeNodeAtIndex(int index) {
156156
return nodeToBeRemoved;
157157
}
158158

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+
159193
public Node getHead() {
160194
System.out.println("Currently head is pointing at node : " + head + " with value: " + head.value);
161195
return this.head;
@@ -205,5 +239,9 @@ public static void main(String[] args) {
205239

206240
linkedList.getHead();
207241
linkedList.getTail();
242+
linkedList.printList();
243+
244+
linkedList.reverse();
245+
linkedList.printList();
208246
}
209247
}

0 commit comments

Comments
 (0)