Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Create and Initialize Doubly Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Create and Initialize Doubly Linked List
Create new node with data
↓
Set node.prev = null
↓
Set node.next = null
↓
Set head = node
↓
Set tail = node
↓
Done: List with one node
Start by making a new node with data. Set its previous and next pointers to null. Then set both head and tail of the list to this node. The list now has one node.
Execution Sample
DSA Typescript
class Node {
  data: number;
  prev: Node | null;
  next: Node | null;
  constructor(data: number) {
    this.data = data; this.prev = null; this.next = null;
  }
}

class DoublyLinkedList {
  head: Node | null = null;
  tail: Node | null = null;
  constructor(data: number) {
    const node = new Node(data);
    this.head = node;
    this.tail = node;
  }
}
This code creates a doubly linked list with one node containing the given data.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create node with data=10NoneNonenull
2Set node.prev = nullNode(10)node.prev = nullNode(10) <-> null
3Set node.next = nullNode(10)node.next = nullnull <-> Node(10) <-> null
4Set head = nodeNode(10)head -> Node(10)head -> Node(10) <-> null
5Set tail = nodeNode(10)tail -> Node(10)head -> Node(10) <-> tail
6DoneNode(10)head and tail point to Node(10)head <-> Node(10) <-> tail
💡 List initialized with one node; head and tail point to the same node.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
node.dataundefined101010101010
node.prevundefinedundefinednullnullnullnullnull
node.nextundefinedundefinedundefinednullnullnullnull
headnullnullnullnullNode(10)Node(10)Node(10)
tailnullnullnullnullnullNode(10)Node(10)
Key Moments - 3 Insights
Why do we set both node.prev and node.next to null when creating the first node?
Because this node is alone, it has no previous or next nodes. Setting both pointers to null shows it is the only node. See steps 2 and 3 in execution_table.
Why do head and tail both point to the same node after initialization?
With only one node, it is both the start and end of the list. So head and tail must point to it. See steps 4 and 5 in execution_table.
What does the visual state 'head <-> Node(10) <-> tail' mean?
It means head and tail both point to the same node with data 10, which has null prev and next pointers. This shows a single-node doubly linked list. See step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the values of node.prev and node.next?
Anode.prev = null, node.next = null
Bnode.prev = Node(10), node.next = null
Cnode.prev = null, node.next = Node(10)
Dnode.prev = undefined, node.next = undefined
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3 in execution_table.
At which step do head and tail both point to the new node?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look at 'Pointer Changes' column for head and tail assignments in execution_table.
If we did not set node.prev to null, what would happen to the list initialization?
AThe tail pointer would be incorrect
BThe list would still work fine with one node
CThe list would have a broken previous pointer causing errors
DThe head pointer would be null
💡 Hint
Refer to key_moments about why node.prev and node.next are set to null.
Concept Snapshot
Create a new node with data.
Set node.prev = null and node.next = null.
Set head and tail pointers to this node.
The list now has one node with no neighbors.
Head and tail point to the same node.
This initializes a doubly linked list.
Full Transcript
To create and initialize a doubly linked list, first make a new node with the given data. This node's previous and next pointers are set to null because it has no neighbors yet. Then, set both the head and tail pointers of the list to this new node. This means the list has exactly one node, which is both the start and end. The visual state shows head and tail pointing to the same node with null neighbors. This process ensures the doubly linked list is ready for further operations.

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