mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Fix LinkedList (#42)
This commit is contained in:
parent
87299a5153
commit
a63bc67cf4
@ -60,7 +60,7 @@ export default class LinkedList {
|
|||||||
let deletedNode = null;
|
let deletedNode = null;
|
||||||
|
|
||||||
// If the head must be deleted then make 2nd node to be a head.
|
// If the head must be deleted then make 2nd node to be a head.
|
||||||
if (this.compare.equal(this.head.value, value)) {
|
while (this.head && this.compare.equal(this.head.value, value)) {
|
||||||
deletedNode = this.head;
|
deletedNode = this.head;
|
||||||
this.head = this.head.next;
|
this.head = this.head.next;
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ describe('LinkedList', () => {
|
|||||||
|
|
||||||
expect(linkedList.delete(5)).toBeNull();
|
expect(linkedList.delete(5)).toBeNull();
|
||||||
|
|
||||||
|
linkedList.append(1);
|
||||||
linkedList.append(1);
|
linkedList.append(1);
|
||||||
linkedList.append(2);
|
linkedList.append(2);
|
||||||
linkedList.append(3);
|
linkedList.append(3);
|
||||||
@ -45,10 +46,10 @@ describe('LinkedList', () => {
|
|||||||
|
|
||||||
const deletedNode = linkedList.delete(3);
|
const deletedNode = linkedList.delete(3);
|
||||||
expect(deletedNode.value).toBe(3);
|
expect(deletedNode.value).toBe(3);
|
||||||
expect(linkedList.toString()).toBe('1,2,4,5');
|
expect(linkedList.toString()).toBe('1,1,2,4,5');
|
||||||
|
|
||||||
linkedList.delete(3);
|
linkedList.delete(3);
|
||||||
expect(linkedList.toString()).toBe('1,2,4,5');
|
expect(linkedList.toString()).toBe('1,1,2,4,5');
|
||||||
|
|
||||||
linkedList.delete(1);
|
linkedList.delete(1);
|
||||||
expect(linkedList.toString()).toBe('2,4,5');
|
expect(linkedList.toString()).toBe('2,4,5');
|
||||||
|
Loading…
Reference in New Issue
Block a user