Skip to content

Commit 73f0b9f

Browse files
committed
improved doubly linked list coverage
1 parent ed0c991 commit 73f0b9f

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

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

+30-39
Original file line numberDiff line numberDiff line change
@@ -78,44 +78,38 @@ export default class DoublyLinkedList {
7878
let deletedNode = null;
7979
let currentNode = this.head;
8080

81-
if (currentNode !== null) {
82-
do {
83-
if (this.compare.equal(currentNode.value, value)) {
84-
deletedNode = currentNode;
85-
86-
if (deletedNode === this.head) {
87-
// set head to second node, which will become new head
88-
this.head = deletedNode.next;
89-
90-
// set new head's previous to null
91-
if (this.head) {
92-
this.head.previous = null;
93-
}
94-
95-
// If all the nodes in list has same value that is passed as argument
96-
// then all nodes will get deleted, therefore tail needs to be updated
97-
if (deletedNode === this.tail) {
98-
this.tail = null;
99-
}
100-
} else if (deletedNode === this.tail) {
101-
// set tail to second last node, which will become new tail
102-
this.tail = deletedNode.previous;
103-
104-
// set new tail's next to null
105-
if (this.tail) {
106-
this.tail.next = null;
107-
}
108-
} else {
109-
const previousNode = deletedNode.previous;
110-
const nextNode = deletedNode.next;
111-
previousNode.next = nextNode;
112-
nextNode.previous = previousNode;
81+
do {
82+
if (this.compare.equal(currentNode.value, value)) {
83+
deletedNode = currentNode;
84+
85+
if (deletedNode === this.head) {
86+
// set head to second node, which will become new head
87+
this.head = deletedNode.next;
88+
89+
// set new head's previous to null
90+
if (this.head) {
91+
this.head.previous = null;
11392
}
93+
94+
// 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
96+
if (deletedNode === this.tail) {
97+
this.tail = null;
98+
}
99+
} else if (deletedNode === this.tail) {
100+
// set tail to second last node, which will become new tail
101+
this.tail = deletedNode.previous;
102+
this.tail.next = null;
103+
} else {
104+
const previousNode = deletedNode.previous;
105+
const nextNode = deletedNode.next;
106+
previousNode.next = nextNode;
107+
nextNode.previous = previousNode;
114108
}
109+
}
115110

116-
currentNode = currentNode.next;
117-
} while (currentNode);
118-
}
111+
currentNode = currentNode.next;
112+
} while (currentNode);
119113

120114
return deletedNode;
121115
}
@@ -166,10 +160,7 @@ export default class DoublyLinkedList {
166160

167161
const deletedTail = this.tail;
168162
this.tail = this.tail.previous;
169-
170-
if (this.tail) {
171-
this.tail.next = null;
172-
}
163+
this.tail.next = null;
173164

174165
return deletedTail;
175166
}

src/data-structures/doubly-linked-list/__test__/DoublyLinkedList.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ describe('DoublyLinkedList', () => {
8989
it('should delete linked list tail', () => {
9090
const linkedList = new DoublyLinkedList();
9191

92+
expect(linkedList.deleteTail()).toBeNull();
93+
9294
linkedList.append(1);
9395
linkedList.append(2);
9496
linkedList.append(3);

0 commit comments

Comments
 (0)