mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add LinkedList.
This commit is contained in:
parent
909976c7bf
commit
53ca279c3b
@ -1,4 +1,4 @@
|
||||
import Node from './Node';
|
||||
import LinkedListNode from './LinkedListNode';
|
||||
|
||||
export default class LinkedList {
|
||||
constructor() {
|
||||
@ -6,7 +6,7 @@ export default class LinkedList {
|
||||
}
|
||||
|
||||
append(value) {
|
||||
const newNode = new Node(value);
|
||||
const newNode = new LinkedListNode(value);
|
||||
|
||||
// If there is no head yet let's make new node a head.
|
||||
if (!this.head) {
|
||||
@ -28,8 +28,8 @@ export default class LinkedList {
|
||||
}
|
||||
|
||||
prepend(value) {
|
||||
const newNode = new Node(value);
|
||||
this.head = new Node(value, this.head);
|
||||
const newNode = new LinkedListNode(value, this.head);
|
||||
this.head = newNode;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
export default class Node {
|
||||
export default class LinkedListNode {
|
||||
constructor(value, next = null) {
|
||||
this.value = value;
|
||||
this.next = next;
|
@ -1,8 +1,8 @@
|
||||
import Node from '../Node';
|
||||
import LinkedListNode from '../LinkedListNode';
|
||||
|
||||
describe('Node', () => {
|
||||
describe('LinkedListNode', () => {
|
||||
it('should create list node with value', () => {
|
||||
const node = new Node(1);
|
||||
const node = new LinkedListNode(1);
|
||||
expect(node.value).toBe(1);
|
||||
expect(node.next).toBeNull();
|
||||
});
|
Loading…
Reference in New Issue
Block a user