- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- Linked Lists Explained
Linked Lists
Linked Lists Explained
Linked lists are asked about far more often than they are used. Learn them because interviews use them to test pointer reasoning, not because your next feature needs one.
The structure
Each node holds a value and a reference to the next node. There is no index and no contiguous block - the nodes can be anywhere in memory, connected by references.
Building and walking a list
javascript
class Node {
constructor(value, next = null) {
this.value = value
this.next = next
}
}
// 1 -> 2 -> 3 -> null
const head = new Node(1, new Node(2, new Node(3)))
function toArray(node) {
const out = []
while (node) {
out.push(node.value)
node = node.next
}
return out
}
console.log(toArray(head)) // [1, 2, 3]