Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d4f9a71

Browse files
fix(linkedlist): removeFirst's last reference
1 parent d4a5037 commit d4f9a71

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ class LinkedList {
183183
this.first = head.next;
184184
if (this.first) {
185185
this.first.previous = null;
186+
} else {
187+
this.last = null;
186188
}
187189
this.size -= 1;
188-
} else {
189-
this.last = null;
190190
}
191191
return head && head.value;
192192
}

‎src/data-structures/linked-lists/linked-list.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ describe('LinkedList Test', () => {
3232
linkedList.addLast('b');
3333
});
3434

35+
it('should remove with only one element', () => {
36+
linkedList = new LinkedList();
37+
linkedList.addLast('a');
38+
39+
expect(linkedList.removeFirst()).toBe('a');
40+
41+
expect(linkedList.size).toBe(0);
42+
expect(linkedList.first).toBe(null);
43+
expect(linkedList.last).toBe(null);
44+
});
45+
3546
it('should remove first item: a', () => {
3647
expect(linkedList.removeFirst()).toBe('a');
3748
expect(linkedList.first.value).toBe('b');
@@ -44,6 +55,7 @@ describe('LinkedList Test', () => {
4455

4556
expect(linkedList.first).toBe(null);
4657
expect(linkedList.last).toBe(null);
58+
expect(linkedList.size).toBe(0);
4759
});
4860
});
4961

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /