Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Insert at End of Doubly Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at End of Doubly Linked List
Create new node with data
↓
Is list empty?
↓
Set head = new
↓
Done
↓
Set last.next = new
↓
Set new.prev = last
↓
Done
Create a new node, check if list is empty. If empty, set head to new node. Otherwise, traverse to last node and link new node at the end.
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;
  }
}

function insertAtEnd(head: Node | null, data: number): Node {
  const newNode = new Node(data);
  if (!head) return newNode;
  let current = head;
  while (current.next) current = current.next;
  current.next = newNode;
  newNode.prev = current;
  return head;
}
This code inserts a new node with given data at the end of a doubly linked list.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Create new node with data=40nullnewNode created with data=40, prev=null, next=nullāˆ… (empty list) or existing list unchanged
2Check if list is emptyhead=null or head=10If head=null, head = newNodeIf empty: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:40│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → newNode
3Traverse to last nodecurrent=head (10)Move current to current.next until current.next=nullStep 3a: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ → │ data:20│ → │ data:30│ │ prev:āˆ… │ │ prev:10│ │ prev:20│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Set last.next = newNodecurrent=30current.next → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ → │ data:20│ → │ data:30│ → │ data:40│ │ prev:āˆ… │ │ prev:10│ │ prev:20│ │ prev:āˆ… │ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Set newNode.prev = currentnewNode=40newNode.prev → current (30)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ → │ data:20│ → │ data:30│ → │ data:40│ │ prev:āˆ… │ │ prev:10│ │ prev:20│ │ prev:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Return headhead=10No pointer changesFinal list: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ → │ data:20│ → │ data:30│ → │ data:40│ │ prev:āˆ… │ │ prev:10│ │ prev:20│ │ prev:30│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
šŸ’” Traversal stops when current.next is null (last node found), then new node linked at end.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5Final
headNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullNode(40)Node(40)Node(40)Node(40)Node(40)
currentnullnullNode(30)Node(30)Node(30)Node(30)
Key Moments - 3 Insights
Why do we need to traverse to the last node before inserting?
Because we must link the new node after the current last node. The traversal stops when current.next is null, as shown in execution_table step 3 and 4.
What happens if the list is empty when inserting?
If the list is empty (head is null), the new node becomes the head directly, as shown in execution_table step 2.
Why do we set newNode.prev to current after linking current.next to newNode?
To maintain the backward link in the doubly linked list. This is shown in execution_table step 5 where newNode.prev points to the last node.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the current node's data?
A20
B30
C10
D40
šŸ’” Hint
Check the 'Current Node' column at step 3 in execution_table.
At which step does the new node's prev pointer get assigned?
AStep 2
BStep 4
CStep 5
DStep 6
šŸ’” Hint
Look at the 'Pointer Changes' column to find when newNode.prev is set.
If the list was empty, what would be the visual state after step 2?
AA single node with data 40, prev and next null, head points to it
BAn empty list with no nodes
CA list with nodes 10, 20, 30, and 40
DA list with nodes 10 and 40 only
šŸ’” Hint
Refer to the 'Visual State' column at step 2 for the empty list case.
Concept Snapshot
Insert at End of Doubly Linked List:
- Create new node with data
- If list empty, set head = new node
- Else traverse to last node
- Set last.next = new node
- Set new node.prev = last node
- Return head of updated list
Full Transcript
To insert at the end of a doubly linked list, first create a new node with the given data. Check if the list is empty by seeing if head is null. If empty, set head to the new node. Otherwise, start from head and move through the list until you reach the last node (where next is null). Then link the new node after the last node by setting last.next to new node and new node.prev to last node. Finally, return the head of the list. This maintains both forward and backward links in the doubly linked list.

Practice

(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To add a new element after the last node
B. To remove the first node
C. To reverse the list
D. To find the middle node

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
  2. Step 2: Purpose of insertion at the end

    Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
  3. Final Answer:

    To add a new element after the last node -> Option A
  4. Quick Check:

    Insert at end = add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insertion at end with deletion
  • Thinking insertion reverses the list
  • Assuming insertion happens at the start
2. Which of the following TypeScript code snippets correctly initializes a new node for a doubly linked list?
easy
A. const newNode = { data: 5, next: undefined };
B. const newNode = { data: 5, next: null, prev: null };
C. const newNode = { value: 5, nextNode: null };
D. const newNode = { data: 5 };

Solution

  1. Step 1: Identify required properties for a doubly linked list node

    A node must have data, next pointer, and prev pointer to link both directions.
  2. Step 2: Check each option for correct properties

    const newNode = { data: 5, next: null, prev: null }; has data, next, and prev all properly set. Others miss prev or use wrong property names.
  3. Final Answer:

    const newNode = { data: 5, next: null, prev: null }; -> Option B
  4. Quick Check:

    Node needs data, next, prev [OK]
Hint: Node needs data, next, and prev properties [OK]
Common Mistakes:
  • Omitting prev pointer
  • Using wrong property names like value or nextNode
  • Setting next or prev as undefined instead of null
3. Given the following TypeScript code for inserting at the end of a doubly linked list, what will be the printed list after insertion?
class Node {
  data: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(data: number) { this.data = data; }
}

class DoublyLinkedList {
  head: Node | null = null;

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

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

const dll = new DoublyLinkedList();
dll.insertAtEnd(10);
dll.insertAtEnd(20);
dll.insertAtEnd(30);
dll.printList();
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. 10 -> 20 -> null
D. null

Solution

  1. Step 1: Trace insertAtEnd calls

    First insert adds 10 as head. Second adds 20 after 10. Third adds 30 after 20.
  2. Step 2: Trace printList output

    Print starts at head (10), then 20, then 30, then null.
  3. Final Answer:

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

    Inserted nodes in order = 10 -> 20 -> 30 [OK]
Hint: Insertion at end appends nodes in order [OK]
Common Mistakes:
  • Confusing order and printing reversed list
  • Missing last inserted node
  • Printing null instead of list
4. Identify the error in the following TypeScript method for inserting at the end of a doubly linked list:
insertAtEnd(data: number) {
  const newNode = new Node(data);
  if (!this.head) {
    this.head = newNode;
  }
  let temp = this.head;
  while (temp.next) {
    temp = temp.next;
  }
  temp.next = newNode;
  newNode.prev = temp;
}
medium
A. newNode should be created outside the method
B. Incorrect assignment of newNode.prev
C. Loop condition should be temp !== null
D. Missing return after setting head when list is empty

Solution

  1. Step 1: Analyze empty list case

    If head is null, method sets head to newNode but continues to loop and assign next, causing error.
  2. Step 2: Fix by adding return after setting head

    Adding return after setting head prevents further code execution and avoids null pointer errors.
  3. Final Answer:

    Missing return after setting head when list is empty -> Option D
  4. Quick Check:

    Return after head set avoids errors [OK]
Hint: Return immediately after setting head if list empty [OK]
Common Mistakes:
  • Not returning after setting head
  • Misunderstanding loop condition
  • Incorrect pointer assignments
5. You want to insert a new node with data 40 at the end of a doubly linked list that currently has nodes 10 <-> 20 <-> 30. After insertion, what will be the correct state of the list including the prev and next pointers of the new node?
hard
A. 10 <-> 20 <-> 30, with 40.prev and 40.next both null (not linked)
B. 40 <-> 10 <-> 20 <-> 30, where 40.next points to 10 and 40.prev is null
C. 10 <-> 20 <-> 30 <-> 40, where 40.prev points to 30 and 40.next is null
D. 10 <-> 20 <-> 30 <-> 40, where 40.prev and 40.next both point to null

Solution

  1. Step 1: Understand insertion at end

    The new node 40 should be linked after 30, so 30.next = 40 and 40.prev = 30.
  2. Step 2: Confirm pointers of new node

    Since 40 is last, 40.next must be null to mark list end.
  3. Final Answer:

    10 <-> 20 <-> 30 <-> 40, where 40.prev points to 30 and 40.next is null -> Option C
  4. Quick Check:

    New tail prev = old tail, next = null [OK]
Hint: New tail node prev = old tail, next = null [OK]
Common Mistakes:
  • Setting new node's next pointer incorrectly
  • Not linking new node's prev pointer
  • Inserting new node at start instead of end
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