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.
A Linked List is a linear data structure where elements (nodes) are connected via pointers/references.
Each node contains:
value: The datanext: Reference to the next nodeOperations:
append(value): Add element to the endlength: Get the number of nodesclass 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