mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Compare commits
5 Commits
da6d01c8fa
...
89bf5e0678
Author | SHA1 | Date | |
---|---|---|---|
|
89bf5e0678 | ||
|
87c1dde2ab | ||
|
a03f49225d | ||
|
dfdb6ec77d | ||
|
019c07e9e3 |
@ -75,6 +75,9 @@ export default class LinkedList {
|
||||
if (currentNode) {
|
||||
newNode.next = currentNode.next;
|
||||
currentNode.next = newNode;
|
||||
if (this.tail === currentNode) {
|
||||
this.tail = newNode;
|
||||
}
|
||||
} else {
|
||||
if (this.tail) {
|
||||
this.tail.next = newNode;
|
||||
|
@ -47,6 +47,18 @@ describe('LinkedList', () => {
|
||||
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', () => {
|
||||
const linkedList = new LinkedList();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user