Skip to content

Commit d0499d2

Browse files
committed
Minor code style fixes for DoublyLinkedList.
1 parent a72fda4 commit d0499d2

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

‎src/data-structures/doubly-linked-list/DoublyLinkedList.js‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,44 @@ export default class DoublyLinkedList{
7878
letdeletedNode=null;
7979
letcurrentNode=this.head;
8080

81-
do{
81+
while(currentNode){
8282
if(this.compare.equal(currentNode.value,value)){
8383
deletedNode=currentNode;
8484

8585
if(deletedNode===this.head){
86+
// If HEAD is going to be deleted...
87+
8688
// Set head to second node, which will become new head.
8789
this.head=deletedNode.next;
8890

89-
// Set new head's previous to null
91+
// Set new head's previous to null.
9092
if(this.head){
9193
this.head.previous=null;
9294
}
9395

9496
// If all the nodes in list has same value that is passed as argument
95-
// then all nodes will get deleted, therefore tail needs to be updated
97+
// then all nodes will get deleted, therefore tail needs to be updated.
9698
if(deletedNode===this.tail){
9799
this.tail=null;
98100
}
99101
}elseif(deletedNode===this.tail){
100-
// set tail to second last node, which will become new tail
102+
// If TAIL is going to be deleted...
103+
104+
// Set tail to second last node, which will become new tail.
101105
this.tail=deletedNode.previous;
102106
this.tail.next=null;
103107
}else{
108+
// If MIDDLE node is going to be deleted...
104109
constpreviousNode=deletedNode.previous;
105110
constnextNode=deletedNode.next;
111+
106112
previousNode.next=nextNode;
107113
nextNode.previous=previousNode;
108114
}
109115
}
110116

111117
currentNode=currentNode.next;
112-
}while(currentNode);
118+
}
113119

114120
returndeletedNode;
115121
}
@@ -149,16 +155,22 @@ export default class DoublyLinkedList{
149155
*/
150156
deleteTail(){
151157
if(!this.tail){
158+
// No tail to delete.
152159
returnnull;
153-
}elseif(this.head===this.tail){
160+
}
161+
162+
if(this.head===this.tail){
163+
// There is only one node in linked list.
154164
constdeletedTail=this.tail;
155165
this.head=null;
156166
this.tail=null;
157167

158168
returndeletedTail;
159169
}
160170

171+
// If there are many nodes in linked list...
161172
constdeletedTail=this.tail;
173+
162174
this.tail=this.tail.previous;
163175
this.tail.next=null;
164176

‎src/data-structures/linked-list/LinkedList.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ export default class LinkedList{
128128
*/
129129
deleteTail(){
130130
if(this.head===this.tail){
131+
// There is only one node in linked list.
131132
constdeletedTail=this.tail;
132133
this.head=null;
133134
this.tail=null;
134135

135136
returndeletedTail;
136137
}
137138

139+
// If there are many nodes in linked list...
138140
constdeletedTail=this.tail;
139141

140142
// Rewind to the last node and delete "next" link for the node before the last one.
@@ -148,6 +150,7 @@ export default class LinkedList{
148150
}
149151

150152
this.tail=currentNode;
153+
151154
returndeletedTail;
152155
}
153156

0 commit comments

Comments
(0)