Bird
0
0
DSA Cprogramming~20 mins

Insert at Specific Position in Doubly Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting at position 3
What is the printed state of the doubly linked list after inserting the value 25 at position 3?
DSA C
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d <-> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

Node* insertAtPosition(Node* head, int data, int pos) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;

    if (pos == 1) {
        newNode->next = head;
        if (head != NULL) head->prev = newNode;
        return newNode;
    }

    Node* temp = head;
    int count = 1;
    while (temp != NULL && count < pos - 1) {
        temp = temp->next;
        count++;
    }

    if (temp == NULL) return head; // position out of bounds

    newNode->next = temp->next;
    if (temp->next != NULL) temp->next->prev = newNode;
    temp->next = newNode;
    newNode->prev = temp;

    return head;
}

int main() {
    Node* head = NULL;
    head = insertAtPosition(head, 10, 1);
    head = insertAtPosition(head, 20, 2);
    head = insertAtPosition(head, 30, 3);
    head = insertAtPosition(head, 25, 3);
    printList(head);
    return 0;
}
A10 <-> 20 <-> 25 <-> 30 <-> NULL
B10 <-> 25 <-> 20 <-> 30 <-> NULL
C10 <-> 20 <-> 30 <-> 25 <-> NULL
D25 <-> 10 <-> 20 <-> 30 <-> NULL
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node goes after the second node.
Predict Output
intermediate
2:00remaining
Output after inserting at position 1 (head)
What is the printed state of the doubly linked list after inserting the value 5 at position 1?
DSA C
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d <-> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

Node* insertAtPosition(Node* head, int data, int pos) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;

    if (pos == 1) {
        newNode->next = head;
        if (head != NULL) head->prev = newNode;
        return newNode;
    }

    Node* temp = head;
    int count = 1;
    while (temp != NULL && count < pos - 1) {
        temp = temp->next;
        count++;
    }

    if (temp == NULL) return head; // position out of bounds

    newNode->next = temp->next;
    if (temp->next != NULL) temp->next->prev = newNode;
    temp->next = newNode;
    newNode->prev = temp;

    return head;
}

int main() {
    Node* head = NULL;
    head = insertAtPosition(head, 10, 1);
    head = insertAtPosition(head, 20, 2);
    head = insertAtPosition(head, 30, 3);
    head = insertAtPosition(head, 5, 1);
    printList(head);
    return 0;
}
A10 <-> 20 <-> 30 <-> 5 <-> NULL
B5 <-> 10 <-> 20 <-> 30 <-> NULL
C10 <-> 5 <-> 20 <-> 30 <-> NULL
D5 <-> 20 <-> 10 <-> 30 <-> NULL
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the new head.
🔧 Debug
advanced
2:30remaining
Identify the bug causing incorrect insertion at tail
The following code attempts to insert a node at a specific position in a doubly linked list. However, inserting at the last position does not link the new node correctly. What is the bug?
DSA C
Node* insertAtPosition(Node* head, int data, int pos) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;

    if (pos == 1) {
        newNode->next = head;
        if (head != NULL) head->prev = newNode;
        return newNode;
    }

    Node* temp = head;
    int count = 1;
    while (temp != NULL && count < pos - 1) {
        temp = temp->next;
        count++;
    }

    if (temp == NULL) return head; // position out of bounds

    newNode->next = temp->next;
    if (temp->next != NULL) temp->next->prev = newNode;
    temp->next = newNode;
    newNode->prev = temp;

    return head;
}
AThe code does not handle insertion when pos is one more than the length, so temp->next is NULL and newNode is not linked properly.
BThe newNode->prev is not set correctly before insertion.
CThe code does not allocate memory for newNode properly.
DThe code does not update head pointer when inserting at the tail.
Attempts:
2 left
💡 Hint
Check what happens when inserting at the end of the list.
Predict Output
advanced
2:00remaining
Output after inserting at position beyond list length
What is the printed state of the doubly linked list after attempting to insert the value 40 at position 10, given the list has only 3 nodes?
DSA C
typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        printf("%d <-> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

Node* insertAtPosition(Node* head, int data, int pos) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;

    if (pos == 1) {
        newNode->next = head;
        if (head != NULL) head->prev = newNode;
        return newNode;
    }

    Node* temp = head;
    int count = 1;
    while (temp != NULL && count < pos - 1) {
        temp = temp->next;
        count++;
    }

    if (temp == NULL) return head; // position out of bounds

    newNode->next = temp->next;
    if (temp->next != NULL) temp->next->prev = newNode;
    temp->next = newNode;
    newNode->prev = temp;

    return head;
}

int main() {
    Node* head = NULL;
    head = insertAtPosition(head, 10, 1);
    head = insertAtPosition(head, 20, 2);
    head = insertAtPosition(head, 30, 3);
    head = insertAtPosition(head, 40, 10);
    printList(head);
    return 0;
}
A40 <-> 10 <-> 20 <-> 30 <-> NULL
B10 <-> 20 <-> 30 <-> 40 <-> NULL
C10 <-> 20 <-> 30 <-> NULL
DNULL
Attempts:
2 left
💡 Hint
Inserting beyond the list length should not change the list.
🧠 Conceptual
expert
1:30remaining
Time complexity of insertion at specific position
What is the time complexity of inserting a node at a specific position in a doubly linked list of length n, assuming you have only the head pointer?
AO(1) because doubly linked lists allow direct access to any position
BO(n^2) because each insertion requires traversing the entire list multiple times
CO(log n) because the list is sorted and binary search can be used
DO(n) because you must traverse the list to reach the position before insertion
Attempts:
2 left
💡 Hint
Think about how you find the position before inserting.