Skip to content

Commit a63bc67

Browse files
m-maksyutintrekhleb
authored andcommittedJun 2, 2018
Fix LinkedList (trekhleb#42)
1 parent 87299a5 commit a63bc67

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class LinkedList {
6060
let deletedNode = null;
6161

6262
// If the head must be deleted then make 2nd node to be a head.
63-
if (this.compare.equal(this.head.value, value)) {
63+
while (this.head && this.compare.equal(this.head.value, value)) {
6464
deletedNode = this.head;
6565
this.head = this.head.next;
6666
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('LinkedList', () => {
3232

3333
expect(linkedList.delete(5)).toBeNull();
3434

35+
linkedList.append(1);
3536
linkedList.append(1);
3637
linkedList.append(2);
3738
linkedList.append(3);
@@ -45,10 +46,10 @@ describe('LinkedList', () => {
4546

4647
const deletedNode = linkedList.delete(3);
4748
expect(deletedNode.value).toBe(3);
48-
expect(linkedList.toString()).toBe('1,2,4,5');
49+
expect(linkedList.toString()).toBe('1,1,2,4,5');
4950

5051
linkedList.delete(3);
51-
expect(linkedList.toString()).toBe('1,2,4,5');
52+
expect(linkedList.toString()).toBe('1,1,2,4,5');
5253

5354
linkedList.delete(1);
5455
expect(linkedList.toString()).toBe('2,4,5');

0 commit comments

Comments
 (0)
Please sign in to comment.