mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 23:12:58 +08:00
Merge 2fe74a7d1e
into 2c67b48c21
This commit is contained in:
commit
3aa5cb5765
@ -12,6 +12,8 @@ export default class LinkedList {
|
|||||||
/** @var LinkedListNode */
|
/** @var LinkedListNode */
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
|
|
||||||
|
this.length = 0;
|
||||||
|
|
||||||
this.compare = new Comparator(comparatorFunction);
|
this.compare = new Comparator(comparatorFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,6 +31,9 @@ export default class LinkedList {
|
|||||||
this.tail = newNode;
|
this.tail = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// once node added, modify the length the list
|
||||||
|
this.length++;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,6 +56,9 @@ export default class LinkedList {
|
|||||||
this.tail.next = newNode;
|
this.tail.next = newNode;
|
||||||
this.tail = newNode;
|
this.tail = newNode;
|
||||||
|
|
||||||
|
// once node added, modify the length the list
|
||||||
|
this.length++;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +71,8 @@ export default class LinkedList {
|
|||||||
const index = rawIndex < 0 ? 0 : rawIndex;
|
const index = rawIndex < 0 ? 0 : rawIndex;
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
this.prepend(value);
|
this.prepend(value);
|
||||||
|
} else if (index === this.length) {
|
||||||
|
this.append(value);
|
||||||
} else {
|
} else {
|
||||||
let count = 1;
|
let count = 1;
|
||||||
let currentNode = this.head;
|
let currentNode = this.head;
|
||||||
@ -85,6 +95,10 @@ export default class LinkedList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// once node added, modify the length the list
|
||||||
|
this.length++;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +139,9 @@ export default class LinkedList {
|
|||||||
this.tail = currentNode;
|
this.tail = currentNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// once node added, modify the length the list
|
||||||
|
this.length--;
|
||||||
|
|
||||||
return deletedNode;
|
return deletedNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,6 +203,9 @@ export default class LinkedList {
|
|||||||
|
|
||||||
this.tail = currentNode;
|
this.tail = currentNode;
|
||||||
|
|
||||||
|
// once node added, modify the length the list
|
||||||
|
this.length--;
|
||||||
|
|
||||||
return deletedTail;
|
return deletedTail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,6 +226,9 @@ export default class LinkedList {
|
|||||||
this.tail = null;
|
this.tail = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// once node added, modify the length the list
|
||||||
|
this.length--;
|
||||||
|
|
||||||
return deletedHead;
|
return deletedHead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user