Bird
Raised Fist0
DSA Typescriptprogramming

Insert at End of Doubly Linked List in DSA Typescript

Choose your learning style10 modes available
Mental Model
A doubly linked list has nodes connected both forward and backward. To add a new node at the end, we link it after the last node and update pointers.
Analogy: Imagine a train where each carriage is connected to the one before and after. Adding a new carriage at the end means hooking it to the last carriage and making sure both know about each other.
null ← 1 ↔ 2 ↔ 3 ↔ null
                 ↑ tail
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, insert value 4 at end
Goal: Add a new node with value 4 at the end of the doubly linked list
Step 1: Create new node with value 4
null ← 1 ↔ 2 ↔ 3 ↔ null   4 (new node, prev=null, next=null)
Why: We need a new node to insert at the end
Step 2: Set new node's prev pointer to current tail (3)
null ← 1 ↔ 2 ↔ 3 ↔ [4] (prev=3, next=null) ↔ null
Why: New node must point back to the last node
Step 3: Set current tail's next pointer to new node
null ← 1 ↔ 2 ↔ 3 ↔ 4 ↔ null
                 ↑ tail now points to 4
Why: Last node must point forward to the new node
Step 4: Update tail pointer to new node
null ← 1 ↔ 2 ↔ 3 ↔ 4 ↔ null
                      ↑ tail
Why: Tail must reflect the new last node
Result:
null ← 1 ↔ 2 ↔ 3 ↔ 4 ↔ null
                      ↑ tail
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;
  }

  insertAtEnd(value: number): void {
    const newNode = new Node(value); // create new node

    if (!this.tail) { // empty list
      this.head = newNode;
      this.tail = newNode;
      return;
    }

    newNode.prev = this.tail; // link new node back to tail
    this.tail.next = newNode; // link tail forward to new node
    this.tail = newNode; // update tail to new node
  }

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

// Driver code
const dll = new DoublyLinkedList();
dll.insertAtEnd(1);
dll.insertAtEnd(2);
dll.insertAtEnd(3);
dll.insertAtEnd(4);
dll.printList();
const newNode = new Node(value); // create new node
create a new node with the given value
if (!this.tail) { this.head = newNode; this.tail = newNode; return; }
handle empty list by setting head and tail to new node
newNode.prev = this.tail; // link new node back to tail
set new node's prev pointer to current tail
this.tail.next = newNode; // link tail forward to new node
set current tail's next pointer to new node
this.tail = newNode; // update tail to new node
update tail pointer to new node
OutputSuccess
1 ↔ 2 ↔ 3 ↔ 4 ↔ null
Complexity Analysis
Time: O(1) because we directly access the tail and update pointers without traversal
Space: O(1) because we only create one new node regardless of list size
vs Alternative: Compared to singly linked list without tail pointer, this is faster since no traversal is needed to find the end
Edge Cases
empty list
new node becomes both head and tail
DSA Typescript
if (!this.tail) { this.head = newNode; this.tail = newNode; return; }
single element list
new node links after the single node, tail updates correctly
DSA Typescript
newNode.prev = this.tail; this.tail.next = newNode; this.tail = newNode;
When to Use This Pattern
When you need to add an element at the end of a list and want fast insertion, use the insert at end pattern with a tail pointer in a doubly linked list.
Common Mistakes
Mistake: Forgetting to update the new node's prev pointer to the current tail
Fix: Always set newNode.prev = this.tail before updating tail.next
Mistake: Not updating the tail pointer to the new node after insertion
Fix: Set this.tail = newNode to keep tail accurate
Summary
Adds a new node at the end of a doubly linked list by updating pointers.
Use when you want to append elements efficiently to the end of a doubly linked list.
The key is to link the new node back to the current tail and update the tail pointer.

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