Bird
Raised Fist0
DSA Typescriptprogramming

Create and Initialize Doubly Linked List in DSA Typescript

Choose your learning style10 modes available
Mental Model
A doubly linked list is a chain of nodes where each node knows the one before and the one after it.
Analogy: Imagine a train where each carriage is connected to the one in front and the one behind, so you can move forward or backward easily.
null ← [head] 1 ↔ 2 ↔ 3 [tail] -> null
Dry Run Walkthrough
Input: Create a doubly linked list with values 1, 2, 3 in order
Goal: Build a doubly linked list where each node points to the previous and next nodes correctly
Step 1: Create first node with value 1; head and tail point to it
null ← [head/tail] 1 -> null
Why: Start the list with the first node; it is both head and tail
Step 2: Create second node with value 2; link it after first node
null ← 1 ↔ [tail] 2 -> null
Why: Add second node after first; update tail to new node
Step 3: Create third node with value 3; link it after second node
null ← 1 ↔ 2 ↔ [tail] 3 -> null
Why: Add third node after second; update tail to new node
Result:
null ← 1 ↔ 2 ↔ 3 -> null
Annotated Code
DSA Typescript
class Node {
  value: number;
  prev: Node | null;
  next: Node | null;

  constructor(value: number) {
    this.value = value;
    this.prev = null;
    this.next = null;
  }
}

class DoublyLinkedList {
  head: Node | null;
  tail: Node | null;

  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value: number): void {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail!.next = newNode; // link current tail to new node
    newNode.prev = this.tail; // link new node back to current tail
    this.tail = newNode; // update tail to new node
  }

  printList(): void {
    let curr = this.head;
    let result = '';
    while (curr) {
      result += curr.value + (curr.next ? ' ↔ ' : ' -> null');
      curr = curr.next;
    }
    console.log(result);
  }
}

// Driver code
const list = new DoublyLinkedList();
list.append(1);
list.append(2);
list.append(3);
list.printList();
if (!this.head) { this.head = newNode; this.tail = newNode; return; }
initialize head and tail when list is empty
this.tail!.next = newNode;
link current tail's next to new node
newNode.prev = this.tail;
link new node's prev to current tail
this.tail = newNode;
update tail pointer to new node
while (curr) { result += curr.value + (curr.next ? ' ↔ ' : ' -> null'); curr = curr.next; }
traverse list to build string showing links
OutputSuccess
1 ↔ 2 ↔ 3 -> null
Complexity Analysis
Time: O(n) to create n nodes because each append adds one node in constant time, repeated n times
Space: O(n) because we store n nodes in memory
vs Alternative: Compared to arrays, doubly linked lists allow easy insertion without shifting elements, but use more memory for pointers
Edge Cases
Empty list before any append
Head and tail are null; first append sets both to new node
DSA Typescript
if (!this.head) { this.head = newNode; this.tail = newNode; return; }
Appending only one node
List has one node with prev and next as null
DSA Typescript
if (!this.head) { this.head = newNode; this.tail = newNode; return; }
When to Use This Pattern
When you need a list where you can move forward and backward easily, use a doubly linked list to keep track of both neighbors.
Common Mistakes
Mistake: Forgetting to update the new node's prev pointer to the old tail
Fix: Always set newNode.prev = this.tail before updating tail
Mistake: Not updating the tail pointer after adding a new node
Fix: Set this.tail = newNode to keep tail accurate
Summary
Creates a doubly linked list where each node points to the previous and next nodes.
Use when you want to traverse a list forwards and backwards efficiently.
Remember to update both next and prev pointers and move the tail pointer when adding nodes.

Practice

(1/5)
1. What is the main difference between a doubly linked list and a singly linked list?
easy
A. A doubly linked list stores data in arrays instead of nodes.
B. A doubly linked list does not use pointers at all.
C. A doubly linked list can only be traversed forward.
D. A doubly linked list has nodes with pointers to both previous and next nodes.

Solution

  1. Step 1: Understand node structure in doubly linked list

    Each node in a doubly linked list contains data and two pointers: one to the previous node and one to the next node.
  2. Step 2: Compare with singly linked list

    A singly linked list node has only one pointer to the next node, so it can only be traversed forward.
  3. Final Answer:

    A doubly linked list has nodes with pointers to both previous and next nodes. -> Option D
  4. Quick Check:

    Doubly linked list = two pointers per node [OK]
Hint: Remember: doubly means two pointers per node [OK]
Common Mistakes:
  • Thinking doubly linked list uses arrays
  • Assuming it can only go forward
  • Confusing pointer usage
2. Which of the following TypeScript code snippets correctly defines a node class for a doubly linked list?
easy
A. class Node { data: number; next: Node; constructor(data: number) { this.data = data; this.next = null; } }
B. class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } }
C. class Node { data: number; prev: Node; constructor(data: number) { this.data = data; this.prev = null; } }
D. class Node { data: number; next: number; prev: number; constructor(data: number) { this.data = data; this.next = 0; this.prev = 0; } }

Solution

  1. Step 1: Check for both next and prev pointers

    class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } defines both next and prev as Node or null, which is correct for doubly linked list nodes.
  2. Step 2: Verify constructor initialization

    class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } initializes next and prev to null, which is proper for a new node.
  3. Final Answer:

    class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } -> Option B
  4. Quick Check:

    Node class needs next and prev pointers [OK]
Hint: Look for both next and prev properties initialized to null [OK]
Common Mistakes:
  • Missing prev pointer in node class
  • Using wrong types for pointers
  • Initializing pointers with numbers instead of null
3. What will be the output of the following TypeScript code that creates a doubly linked list with nodes 10, 20, 30 and prints the list forward?
class Node {
  data: number;
  next: Node | null;
  prev: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  head: Node | null = null;

  append(data: number) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
    newNode.prev = current;
  }

  printForward() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.data + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new DoublyLinkedList();
list.append(10);
list.append(20);
list.append(30);
list.printForward();
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. null -> 10 -> 20 -> 30
D. 10 -> 20 -> null

Solution

  1. Step 1: Append nodes 10, 20, 30 in order

    Nodes are added at the end, so list order is 10, then 20, then 30.
  2. Step 2: Print list forward

    printForward traverses from head to next nodes, printing data with arrows, ending with null.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option A
  4. Quick Check:

    Appending adds to tail, printForward shows 10 to 30 [OK]
Hint: Appending adds nodes at tail; printForward shows head to tail [OK]
Common Mistakes:
  • Confusing print order as reversed
  • Missing last node in output
  • Printing null at start instead of end
4. Identify the error in the following TypeScript code that tries to append a node to a doubly linked list:
class Node {
  data: number;
  next: Node | null;
  prev: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  head: Node | null = null;

  append(data: number) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
    // Missing line here
  }
}
medium
A. The code misses setting newNode.prev to current before appending.
B. The code should set head to newNode even if head exists.
C. The constructor should initialize next and prev to undefined, not null.
D. The while loop should check current.prev instead of current.next.

Solution

  1. Step 1: Check append logic for doubly linked list

    When appending, newNode.prev must point to current (the last node) to maintain backward link.
  2. Step 2: Identify missing pointer assignment

    The code sets current.next = newNode but does not set newNode.prev = current, breaking backward link.
  3. Final Answer:

    The code misses setting newNode.prev to current before appending. -> Option A
  4. Quick Check:

    newNode.prev must link to previous node [OK]
Hint: Always set newNode.prev = last node when appending [OK]
Common Mistakes:
  • Forgetting to set newNode.prev
  • Changing head incorrectly
  • Checking wrong pointer in loop
5. You want to create a doubly linked list from an array of numbers [5, 15, 25] and then reverse the list by changing pointers (not data). Which sequence of pointer changes correctly reverses the list?
hard
A. For each node, set next to prev and leave prev unchanged; head remains same.
B. For each node, set next to null and prev to null; then append nodes in reverse order.
C. For each node, swap its next and prev pointers; then update head to last node.
D. For each node, swap data values instead of pointers; head stays unchanged.

Solution

  1. Step 1: Understand reversing a doubly linked list by pointers

    Reversing means for each node, next and prev pointers must be swapped to reverse direction.
  2. Step 2: Update head pointer

    After swapping pointers, head must point to the original tail (last node) to reflect new start.
  3. Final Answer:

    For each node, swap its next and prev pointers; then update head to last node. -> Option C
  4. Quick Check:

    Swap next and prev, then reset head [OK]
Hint: Swap next and prev pointers on all nodes, then reset head [OK]
Common Mistakes:
  • Only swapping data, not pointers
  • Not updating head after reversal
  • Setting pointers to null incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard