Doubly Linked List Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Doubly Linked List runs in O(1) insert time and O(n) space, in TypeScript as in every other language.
- ✓Nothing at runtime beyond what JavaScript provides, so the structure is the same objects. What TypeScript adds is the ability to make the null-terminated shape explicit: type Node<T> = { value: T; prev: Node<T> | null; next: Node<T> | null } is a legal recursive type, and interfaces may reference themselves without any indirection.
- ✓Use sentinel nodes typed as non-nullable and keep the union out of the hot path. If head and tail are sentinels, every interior link is provably non-null, so the internal node type can be Node<T> with no "| null" and the assertions disappear from insert and remove entirely.
- ✓The TypeScript trap: Control-flow narrowing on a property is discarded by any intervening function call. Writing if (node.next) { doSomething(); node.next.value } fails to compile under strictNullChecks, because TypeScript must assume doSomething mutated node.next.
- ✓Types are erased entirely, so the emitted code and its performance are identical to the JavaScript version.
What Doubly Linked List actually is
Doubly Linked List in TypeScript: what you actually reach for
TypeScript implementation
interface DllNode<T> {
value: T;
prev: DllNode<T>; // non-null: sentinels bound both ends
next: DllNode<T>;
}
export class DoublyLinkedList<T> {
private readonly head: DllNode<T>;
private readonly tail: DllNode<T>;
private count = 0;
constructor() {
// The sentinels are the only nodes whose value is not a T.
this.head = { value: undefined as unknown as T } as DllNode<T>;
this.tail = { value: undefined as unknown as T } as DllNode<T>;
this.head.next = this.tail;
this.tail.prev = this.head;
}
append(value: T): DllNode<T> {
const node = { value } as DllNode<T>;
const last = this.tail.prev;
last.next = node;
node.prev = last;
node.next = this.tail;
this.tail.prev = node;
this.count++;
return node;
}
remove(node: DllNode<T>): void {
// Bind once. Reading node.next twice around a call would lose narrowing.
const { prev, next } = node;
prev.next = next;
next.prev = prev;
this.count--;
}
get size(): number {
return this.count;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(1) insert
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Doubly Linked List?
Nothing at runtime beyond what JavaScript provides, so the structure is the same objects. What TypeScript adds is the ability to make the null-terminated shape explicit: type Node<T> = { value: T; prev: Node<T> | null; next: Node<T> | null } is a legal recursive type, and interfaces may reference themselves without any indirection.
What is the most common TypeScript mistake when implementing Doubly Linked List?
Control-flow narrowing on a property is discarded by any intervening function call. Writing if (node.next) { doSomething(); node.next.value } fails to compile under strictNullChecks, because TypeScript must assume doSomething mutated node.next. The fix is to bind it once, const next = node.next, and narrow the local. Developers usually reach for the non-null assertion operator instead, which silences the compiler and reintroduces exactly the runtime crash the flag exists to prevent.
How does Doubly Linked List actually perform in TypeScript?
Types are erased entirely, so the emitted code and its performance are identical to the JavaScript version. The only cost is compile time, and on a recursive generic like this it is negligible. Time complexity is O(1) insert and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what GitHub Copilot offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.