Skip to content

Commit a3ca7bb

Browse files
committed
added constraint - empty list
1 parent e52f99f commit a3ca7bb

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

chapter05/03-Doubly-Linked-List.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ function DoublyLinkedList() {
4242

4343
if (position === 0){ //add on first position
4444

45-
node.next = current;
46-
current.prev = node; //NEW {1}
47-
head = node;
45+
if (!head){ //NEW
46+
head = node;
47+
tail = node;
48+
} else {
49+
node.next = current;
50+
current.prev = node; //NEW {1}
51+
head = node;
52+
}
4853

49-
} else if (position === length-1) { //last item //NEW
54+
} else if (position === length) { //last item //NEW
5055

5156
current = tail; // {2}
5257
current.next = node;

0 commit comments

Comments
 (0)