Bird
Raised Fist0
DSA Cprogramming

Why Doubly Linked List Over Singly Linked List in DSA C - Why This Pattern

Choose your learning style10 modes available
Mental Model
A doubly linked list lets you move forward and backward easily, unlike a singly linked list which only moves forward.
Analogy: Think of a train with cars connected both ways so you can walk forward or backward between cars, versus a train where you can only walk forward.
Singly Linked List:
head -> [1] -> [2] -> [3] -> null

Doubly Linked List:
null ← [1] ↔ [2] ↔ [3] -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, delete node 2
Goal: Show how deleting a middle node is easier with a doubly linked list than with a singly linked list
Step 1: In singly linked list, find node before 2 (node 1) to delete node 2
head -> [1] -> [2] -> [3] -> null
Why: We need the previous node to change its next pointer to skip node 2
Step 2: Change node 1's next pointer to node 3, skipping node 2
head -> [1] -> [3] -> null
Why: This removes node 2 from the chain
Step 3: In doubly linked list, directly access node 2 and update pointers
null ← [1] ↔ [2] ↔ [3] -> null
Why: Node 2 has pointers to both previous and next nodes
Step 4: Set node 1's next to node 3 and node 3's prev to node 1, removing node 2
null ← [1] ↔ [3] -> null
Why: Both forward and backward links are updated easily without traversal
Result:
Singly Linked List after deletion:
head -> [1] -> [3] -> null

Doubly Linked List after deletion:
null ← [1] ↔ [3] -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Singly linked list node
typedef struct SNode {
    int data;
    struct SNode* next;
} SNode;

// Doubly linked list node
typedef struct DNode {
    int data;
    struct DNode* prev;
    struct DNode* next;
} DNode;

// Create singly linked list 1->2->3
SNode* create_singly_list() {
    SNode* head = malloc(sizeof(SNode));
    head->data = 1;
    head->next = malloc(sizeof(SNode));
    head->next->data = 2;
    head->next->next = malloc(sizeof(SNode));
    head->next->next->data = 3;
    head->next->next->next = NULL;
    return head;
}

// Create doubly linked list 1<->2<->3
DNode* create_doubly_list() {
    DNode* n1 = malloc(sizeof(DNode));
    DNode* n2 = malloc(sizeof(DNode));
    DNode* n3 = malloc(sizeof(DNode));
    n1->data = 1; n1->prev = NULL; n1->next = n2;
    n2->data = 2; n2->prev = n1; n2->next = n3;
    n3->data = 3; n3->prev = n2; n3->next = NULL;
    return n1;
}

// Delete node with value 2 from singly linked list
void delete_singly(SNode** head_ref, int val) {
    SNode* curr = *head_ref;
    SNode* prev = NULL;
    while (curr && curr->data != val) {
        prev = curr; // move prev forward
        curr = curr->next; // move curr forward
    }
    if (!curr) return; // not found
    if (!prev) { // deleting head
        *head_ref = curr->next;
    } else {
        prev->next = curr->next; // bypass curr
    }
    free(curr);
}

// Delete node with value 2 from doubly linked list
void delete_doubly(DNode** head_ref, int val) {
    DNode* curr = *head_ref;
    while (curr && curr->data != val) {
        curr = curr->next; // move curr forward
    }
    if (!curr) return; // not found
    if (curr->prev) {
        curr->prev->next = curr->next; // link prev to next
    } else {
        *head_ref = curr->next; // deleting head
    }
    if (curr->next) {
        curr->next->prev = curr->prev; // link next to prev
    }
    free(curr);
}

// Print singly linked list
void print_singly(SNode* head) {
    SNode* curr = head;
    while (curr) {
        printf("%d -> ", curr->data);
        curr = curr->next;
    }
    printf("null\n");
}

// Print doubly linked list
void print_doubly(DNode* head) {
    DNode* curr = head;
    while (curr) {
        printf("%d", curr->data);
        if (curr->next) printf(" <-> ");
        curr = curr->next;
    }
    printf("\n");
}

int main() {
    // Singly linked list test
    SNode* s_head = create_singly_list();
    printf("Singly Linked List before deletion:\n");
    print_singly(s_head);
    delete_singly(&s_head, 2);
    printf("Singly Linked List after deleting 2:\n");
    print_singly(s_head);

    // Doubly linked list test
    DNode* d_head = create_doubly_list();
    printf("Doubly Linked List before deletion:\n");
    print_doubly(d_head);
    delete_doubly(&d_head, 2);
    printf("Doubly Linked List after deleting 2:\n");
    print_doubly(d_head);

    return 0;
}
while (curr && curr->data != val) { prev = curr; // move prev forward curr = curr->next; // move curr forward }
Traverse singly list to find node to delete and keep track of previous node
if (!prev) { // deleting head *head_ref = curr->next; } else { prev->next = curr->next; // bypass curr }
Update previous node's next pointer to skip the deleted node
while (curr && curr->data != val) { curr = curr->next; // move curr forward }
Traverse doubly list to find node to delete
if (curr->prev) { curr->prev->next = curr->next; // link prev to next } else { *head_ref = curr->next; // deleting head } if (curr->next) { curr->next->prev = curr->prev; // link next to prev }
Update both previous and next nodes to bypass the deleted node
OutputSuccess
Singly Linked List before deletion: 1 -> 2 -> 3 -> null Singly Linked List after deleting 2: 1 -> 3 -> null Doubly Linked List before deletion: 1 <-> 2 <-> 3 Doubly Linked List after deleting 2: 1 <-> 3
Complexity Analysis
Time: O(n) because we may need to traverse the list to find the node to delete
Space: O(1) because deletion uses constant extra space
vs Alternative: Doubly linked list allows backward pointer updates without extra traversal, making some operations like deletion easier compared to singly linked list which requires tracking previous node
Edge Cases
Deleting head node
Head pointer is updated to next node correctly
DSA C
if (!prev) { // deleting head
        *head_ref = curr->next;
Deleting tail node
Next pointer of previous node is set to null; doubly list updates prev pointer of next node if exists
DSA C
if (curr->next) {
        curr->next->prev = curr->prev;
Deleting node not found
Function returns without changes
DSA C
if (!curr) return; // not found
When to Use This Pattern
When you need to delete or insert nodes easily from both ends or in the middle without traversing from head, reach for doubly linked list because it has backward pointers.
Common Mistakes
Mistake: In singly linked list deletion, forgetting to track the previous node causes loss of list links.
Fix: Always keep a pointer to the previous node while traversing to update its next pointer.
Mistake: In doubly linked list deletion, forgetting to update both prev and next pointers breaks the list.
Fix: Update both previous node's next and next node's prev pointers to maintain list integrity.
Summary
Doubly linked list allows easy movement and updates in both directions.
Use it when you need efficient backward traversal or easy deletion of nodes without extra traversal.
The key insight is that having pointers both ways removes the need to find previous nodes during operations.

Practice

(1/5)
1. Why might a doubly linked list be preferred over a singly linked list?
easy
A. Because it cannot be modified after creation
B. Because it uses less memory than singly linked list
C. Because it only stores one pointer per node
D. Because it allows traversal in both forward and backward directions

Solution

  1. Step 1: Understand traversal capabilities

    A singly linked list allows traversal only in one direction, from head to tail.
  2. Step 2: Compare with doubly linked list

    A doubly linked list stores two pointers per node, allowing traversal forward and backward.
  3. Final Answer:

    Because it allows traversal in both forward and backward directions -> Option D
  4. Quick Check:

    Traversal direction = Both [OK]
Hint: Doubly linked lists have two pointers per node [OK]
Common Mistakes:
  • Thinking doubly linked lists use less memory
  • Confusing traversal directions
  • 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 C?
easy
A. struct Node { int data; struct Node* prev; };
B. struct Node { int data; struct Node* next; };
C. struct Node { int data; struct Node* prev; struct Node* next; };
D. struct Node { int data; int next; int prev; };

Solution

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

    A doubly linked list node must have pointers to both previous and next nodes.
  2. Step 2: Check each option

    struct Node { int data; struct Node* prev; struct Node* next; }; correctly declares both prev and next pointers as struct Node pointers.
  3. Final Answer:

    struct Node { int data; struct Node* prev; struct Node* next; }; -> Option C
  4. Quick Check:

    Node pointers = prev and next [OK]
Hint: Doubly linked nodes have both prev and next pointers [OK]
Common Mistakes:
  • Omitting prev pointer
  • Using int instead of pointer for next/prev
  • Confusing singly linked list node with doubly linked
3. What will be the output of the following code snippet that uses a doubly linked list?
typedef struct Node {
  int data;
  struct Node* prev;
  struct Node* next;
} Node;

Node* head = NULL;

// Function to add node at end
void append(int val) {
  Node* newNode = (Node*)malloc(sizeof(Node));
  newNode->data = val;
  newNode->next = NULL;
  if (head == NULL) {
    newNode->prev = NULL;
    head = newNode;
    return;
  }
  Node* temp = head;
  while (temp->next != NULL) temp = temp->next;
  temp->next = newNode;
  newNode->prev = temp;
}

// Print list forward
void printForward() {
  Node* temp = head;
  while (temp != NULL) {
    printf("%d ", temp->data);
    temp = temp->next;
  }
}

int main() {
  append(10);
  append(20);
  append(30);
  printForward();
  return 0;
}
medium
A. 10 20 30
B. 30 20 10
C. 10 20
D. Compilation error

Solution

  1. Step 1: Trace append function calls

    append(10) creates first node with data 10, head points to it. append(20) adds node with 20 at end. append(30) adds node with 30 at end.
  2. Step 2: Trace printForward function

    printForward starts at head and prints data while moving next. It prints 10, then 20, then 30.
  3. Final Answer:

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

    List forward print = 10 20 30 [OK]
Hint: Forward print shows nodes in insertion order [OK]
Common Mistakes:
  • Confusing forward and backward traversal
  • Assuming printForward prints in reverse
  • Missing NULL checks causing errors
4. Identify the bug in the following code snippet that tries to delete the last node of a doubly linked list:
void deleteLast() {
  if (head == NULL) return;
  Node* temp = head;
  while (temp->next != NULL) {
    temp = temp->next;
  }
  temp->prev->next = NULL;
  free(temp);
}
medium
A. No bug, code works correctly
B. Does not handle case when list has only one node
C. Incorrect pointer used for deletion
D. Memory leak due to missing free

Solution

  1. Step 1: Analyze deletion logic

    The code finds last node by moving to node where next is NULL, then sets previous node's next to NULL and frees last node.
  2. Step 2: Check edge case for single node list

    If list has only one node, temp->prev is NULL, so temp->prev->next causes a crash or undefined behavior.
  3. Final Answer:

    Does not handle case when list has only one node -> Option B
  4. Quick Check:

    Single node deletion needs special handling [OK]
Hint: Check for single node before accessing prev pointer [OK]
Common Mistakes:
  • Ignoring single node edge case
  • Assuming prev pointer always exists
  • Not freeing memory after deletion
5. You want to implement a feature that allows moving backward through a list of recently opened files in a text editor. Which linked list type is best and why?
hard
A. Doubly linked list, because it allows moving backward and forward easily
B. Singly linked list, because it uses less memory and is simpler
C. Circular singly linked list, because it loops infinitely
D. Array, because linked lists cannot move backward

Solution

  1. Step 1: Understand feature requirements

    The feature requires moving backward and forward through the list of files.
  2. Step 2: Match data structure capabilities

    Singly linked lists allow only forward traversal, arrays allow random access but not efficient insertion/deletion, doubly linked lists allow easy backward and forward traversal.
  3. Step 3: Evaluate options

    Doubly linked list fits best because it supports moving backward and forward efficiently.
  4. Final Answer:

    Doubly linked list, because it allows moving backward and forward easily -> Option A
  5. Quick Check:

    Backward traversal = Doubly linked list [OK]
Hint: Backward navigation needs doubly linked list [OK]
Common Mistakes:
  • Choosing singly linked list for backward moves
  • Ignoring traversal needs
  • Assuming arrays handle backward traversal efficiently
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