JS Built-ins
Algorithmsintermediate

Linked List: Append and Length

πŸ“– Usage Example
class Node {
  constructor(val) { this.val = val; this.next = null; }
}
const head = new Node(1);
head.next = new Node(2);

A linked list chains nodes together via next pointers instead of an array.

Linked List Data Structure

A Linked List is a linear data structure where elements (nodes) are connected via pointers/references.

Each node contains:

  • value: The data
  • next: Reference to the next node

Operations:

  • append(value): Add element to the end
  • length: Get the number of nodes
class Node {
  constructor(value: any) {
    this.value = value
    this.next = null
  }
}

class LinkedList {
  append(value: any): void {}
  getLength(): number {}
}

const ll = new LinkedList()
ll.append(1)
ll.append(2)
ll.getLength() // 2
πŸ’‘ Hints (4)
  • β€’ Create a new Node with the value.
  • β€’ If head is null, set it as the new node.
  • β€’ Otherwise, traverse to the end and append there.
  • β€’ For getLength, traverse the entire list and count nodes.