Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Why Doubly Linked List Over Singly Linked List in DSA Typescript - Why It Works

Choose your learning style10 modes available
Concept Flow - Why Doubly Linked List Over Singly Linked List
Start with List
↓
Is backward traversal needed?
No→Use Singly Linked List
Yes↓
Use Doubly Linked List
↓
Supports forward and backward traversal
↓
More memory used for prev pointer
↓
Better for operations needing backward access
↓
End
Decide if backward traversal or easier node deletion is needed; if yes, use doubly linked list, else singly linked list.
Execution Sample
DSA Typescript
class Node {
  data: number;
  next: Node | null = null;
  prev: Node | null = null;
}

// Doubly linked list allows backward traversal
Defines a doubly linked list node with next and prev pointers for two-way traversal.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create node AAhead -> A, tail -> AA(prev: null, next: null)
2Add node B after AA -> BA.next -> B, B.prev -> A, tail -> BA(prev: null, next: B) <-> B(prev: A, next: null)
3Add node C after BA -> B -> CB.next -> C, C.prev -> B, tail -> CA <-> B <-> C
4Traverse forward from headA -> B -> CNo pointer changeOutput: A -> B -> C
5Traverse backward from tailA -> B -> CNo pointer changeOutput: C -> B -> A
6Delete node BA -> CA.next -> C, C.prev -> AA <-> C
7Try backward traversal after deletionA -> CNo pointer changeOutput: C -> A
8EndA -> CNo pointer changeFinal list: A <-> C
💡 Traversal and deletion demonstrate backward access benefits; doubly linked list used for two-way navigation.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
headnullAAAAAAA
tailnullABCCCCC
A.nextnullnullBBBBCC
A.prevnullnullnullnullnullnullnullnull
B.nextnullnullnullCCCnullnull
B.prevnullnullAAAAnullnull
C.nextnullnullnullnullnullnullnullnull
C.prevnullnullnullBBBAA
Key Moments - 3 Insights
Why do we need the 'prev' pointer in a doubly linked list?
The 'prev' pointer allows moving backward through the list, as shown in steps 5 and 7 where backward traversal is possible, unlike singly linked lists.
How does deletion of a middle node differ between singly and doubly linked lists?
In step 6, deletion updates both next and prev pointers to bypass node B easily. In singly linked lists, you must traverse from head to find the previous node to update its next pointer.
Does using a doubly linked list use more memory?
Yes, each node stores an extra 'prev' pointer, increasing memory usage, but it enables backward traversal and easier node deletion as seen in the execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 5, what is the output of backward traversal?
AA -> B -> C
BA -> C
CC -> B -> A
DC -> A
💡 Hint
Check the 'Visual State' and 'Operation' columns at step 5 for backward traversal output.
At which step is node B deleted from the list?
AStep 6
BStep 7
CStep 4
DStep 3
💡 Hint
Look for the 'Delete node B' operation in the execution table.
If the 'prev' pointer was removed, which operation would be affected?
AAdding nodes at the end
BBackward traversal
CForward traversal
DCreating the first node
💡 Hint
Refer to steps 5 and 7 where backward traversal uses the 'prev' pointer.
Concept Snapshot
Doubly Linked List stores two pointers per node: next and prev.
Allows traversal forward and backward.
Easier deletion of nodes without full traversal.
Uses more memory than singly linked list.
Best when backward access or frequent deletions needed.
Full Transcript
This concept explains why doubly linked lists are used over singly linked lists. Doubly linked lists have two pointers per node: one to the next node and one to the previous node. This allows moving forward and backward through the list easily. The execution steps show creating nodes, adding nodes, traversing forward and backward, and deleting a node. Backward traversal and easier deletion are the main advantages. However, doubly linked lists use more memory because of the extra pointer. The key moments clarify why the prev pointer is important, how deletion is simpler, and the memory trade-off. The quiz tests understanding of traversal outputs, deletion steps, and the role of the prev pointer.

Practice

(1/5)
1. Why might you choose a doubly linked list over a singly linked list in a program?
easy
A. Because it uses less memory per node
B. Because it allows traversal in both directions easily
C. Because it is simpler to implement
D. Because it only stores one pointer per node

Solution

  1. Step 1: Understand traversal capabilities

    A singly linked list allows moving forward only, while a doubly linked list allows moving forward and backward.
  2. Step 2: Compare memory and complexity

    Doubly linked lists use more memory and are more complex but provide two-way traversal.
  3. Final Answer:

    Because it allows traversal in both directions easily -> Option B
  4. Quick Check:

    Two-way traversal = A [OK]
Hint: Doubly means two directions, singly means one [OK]
Common Mistakes:
  • Thinking doubly linked lists use less memory
  • Confusing simplicity with singly linked lists
  • Assuming singly linked lists allow backward traversal
2. Which of the following is the correct way to declare a node in a doubly linked list in TypeScript?
easy
A. class Node { value: number; next: Node; }
B. class Node { value: number; prev: Node; }
C. class Node { value: number; next: Node | null; prev: Node | null; }
D. class Node { value: number; next: Node | null; }

Solution

  1. Step 1: Identify required pointers for doubly linked list node

    A doubly linked list node must have both next and prev pointers, which can be null if no neighbor exists.
  2. Step 2: Check TypeScript syntax for nullable pointers

    Using Node | null correctly allows for the end nodes to have null pointers.
  3. Final Answer:

    class Node { value: number; next: Node | null; prev: Node | null; } -> Option C
  4. Quick Check:

    Doubly linked node needs next and prev with nulls [OK]
Hint: Doubly linked nodes have next and prev pointers [OK]
Common Mistakes:
  • Omitting the prev pointer
  • Not allowing null for next or prev
  • Using only one pointer like singly linked list
3. Consider this TypeScript code snippet for a doubly linked list node insertion at the front:
class Node {
  value: number;
  next: Node | null = null;
  prev: Node | null = null;
  constructor(value: number) { this.value = value; }
}

class DoublyLinkedList {
  head: Node | null = null;
  insertFront(value: number) {
    const newNode = new Node(value);
    if (this.head) {
      newNode.next = this.head;
      this.head.prev = newNode;
    }
    this.head = newNode;
  }
}

const list = new DoublyLinkedList();
list.insertFront(10);
list.insertFront(20);
list.insertFront(30);

let current = list.head;
const result = [];
while (current) {
  result.push(current.value);
  current = current.next;
}
console.log(result);
What will be printed?
medium
A. [10, 30, 20]
B. [10, 20, 30]
C. [20, 30, 10]
D. [30, 20, 10]

Solution

  1. Step 1: Trace insertFront calls

    First insert 10: list is 10. Then insert 20 at front: list is 20 -> 10. Then insert 30 at front: list is 30 -> 20 -> 10.
  2. Step 2: Traverse from head to next

    Starting at head (30), next is 20, then 10, then null. So collected values are [30, 20, 10].
  3. Final Answer:

    [30, 20, 10] -> Option D
  4. Quick Check:

    Insert front reverses order = [30, 20, 10] [OK]
Hint: Insert front adds new node before head [OK]
Common Mistakes:
  • Assuming insertion appends at end
  • Mixing order of traversal
  • Ignoring prev pointer effects
4. This TypeScript code tries to delete the last node from a doubly linked list but has a bug:
deleteLast() {
  if (!this.head) return;
  let current = this.head;
  while (current.next) {
    current = current.next;
  }
  current.prev.next = null;
}
What is the bug here?
medium
A. It does not handle the case when list has only one node
B. It incorrectly sets current.next to null instead of current.prev.next
C. It causes infinite loop in the while
D. It deletes the head node instead of the last node

Solution

  1. Step 1: Analyze deletion logic

    The code moves to the last node by following next pointers. Then it sets current.prev.next = null to remove last node.
  2. Step 2: Check edge case for single node list

    If list has only one node, current.prev is null, so current.prev.next causes error.
  3. Final Answer:

    It does not handle the case when list has only one node -> Option A
  4. Quick Check:

    Single node list needs special case [OK]
Hint: Check for single node before accessing prev [OK]
Common Mistakes:
  • Ignoring single node edge case
  • Assuming prev is always non-null
  • Confusing next and prev pointers
5. You want to implement a feature that requires frequent backward traversal from any node in a list. Which data structure is best and why? Options: A) Singly linked list because it uses less memory B) Circular singly linked list because it loops infinitely C) Array because it supports random access D) Doubly linked list because it allows backward traversal efficiently
hard
A. Doubly linked list because it allows backward traversal efficiently
B. Circular singly linked list because it loops infinitely
C. Array because it supports random access
D. Singly linked list because it uses less memory

Solution

  1. Step 1: Understand backward traversal need

    Backward traversal means moving from a node to its previous nodes efficiently.
  2. Step 2: Compare data structures for backward traversal

    Singly linked lists do not have backward pointers. Arrays allow random access but not linked traversal. Circular singly linked lists loop but do not support backward traversal. Doubly linked lists have prev pointers for efficient backward moves.
  3. Final Answer:

    Doubly linked list because it allows backward traversal efficiently -> Option A
  4. Quick Check:

    Backward traversal needs prev pointers [OK]
Hint: Backward moves need prev pointers, only doubly linked lists have them [OK]
Common Mistakes:
  • Choosing singly linked list for backward traversal
  • Confusing arrays with linked lists
  • Thinking circular singly linked lists support backward moves
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