The add method is created here to add value to a linked list.
add
class Node { constructor(data) { this.data = data this.next = null } } class LinkedList { constructor(head) { this.head = head } append = (value) => { const newNode = new Node(value) let current = this.head if (!this.head) { this.head = newNode return } while (current.next) { current = current.next } current.next = newNode } }
Last updated 2 years ago
Was this helpful?