mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Merge 87c1dde2ab
into ca3d16dcce
This commit is contained in:
commit
da6d01c8fa
@ -75,6 +75,9 @@ export default class LinkedList {
|
|||||||
if (currentNode) {
|
if (currentNode) {
|
||||||
newNode.next = currentNode.next;
|
newNode.next = currentNode.next;
|
||||||
currentNode.next = newNode;
|
currentNode.next = newNode;
|
||||||
|
if (this.tail === currentNode) {
|
||||||
|
this.tail = newNode;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.tail) {
|
if (this.tail) {
|
||||||
this.tail.next = newNode;
|
this.tail.next = newNode;
|
||||||
|
@ -47,6 +47,18 @@ describe('LinkedList', () => {
|
|||||||
expect(linkedList.toString()).toBe('1,4,2,3,10');
|
expect(linkedList.toString()).toBe('1,4,2,3,10');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should insert and maintain head and tail', () => {
|
||||||
|
const linkedList = new LinkedList();
|
||||||
|
|
||||||
|
linkedList.insert(2, 0);
|
||||||
|
linkedList.insert(3, 1);
|
||||||
|
linkedList.insert(4, 2);
|
||||||
|
|
||||||
|
expect(linkedList.toString()).toBe('2,3,4');
|
||||||
|
expect(linkedList.head.toString()).toBe('2');
|
||||||
|
expect(linkedList.tail.toString()).toBe('4');
|
||||||
|
});
|
||||||
|
|
||||||
it('should delete node by value from linked list', () => {
|
it('should delete node by value from linked list', () => {
|
||||||
const linkedList = new LinkedList();
|
const linkedList = new LinkedList();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user