Insert at End of Doubly Linked List in DSA C - Time & Space Complexity
We want to understand how the time needed to add a new item at the end of a doubly linked list changes as the list grows.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
This code adds a new node at the end of a doubly linked list by walking through the list to find the last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the list nodes to find the last node.
- How many times: It runs once for each node in the list until the last one is found, so roughly n times for a list of size n.
As the list gets longer, the time to find the end grows roughly in a straight line with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to reach the end |
| 100 | About 100 steps to reach the end |
| 1000 | About 1000 steps to reach the end |
Pattern observation: The steps increase directly with the number of nodes, so doubling the list size roughly doubles the work.
Time Complexity: O(n)
This means the time to insert at the end grows linearly with the list size because we must walk through all nodes to find the end.
[X] Wrong: "Inserting at the end is always fast and takes the same time no matter the list size."
[OK] Correct: Without a pointer to the last node, we must walk through the whole list, so bigger lists take more time.
Understanding this helps you explain how linked lists work and why keeping track of the last node can speed up insertions.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand the list structure
A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.Step 2: Purpose of insertion at end
Inserting at the end means adding a new node after the last node, extending the list.Final Answer:
To add a new element after the last node -> Option AQuick Check:
Insert at end = add after last node [OK]
- Confusing insertion at end with deletion
- Thinking insertion happens at the start
- Mixing up singly and doubly linked list operations
Solution
Step 1: Identify the last node's next pointer
The last node's 'next' pointer should point to the new node to link it at the end.Step 2: Correct pointer assignment
Assigning 'last->next = newNode;' correctly links the new node after the last node.Final Answer:
last->next = newNode; -> Option AQuick Check:
Last node's next points to new node [OK]
- Assigning newNode->next to last (wrong direction)
- Changing last->prev instead of last->next
- Setting newNode->prev to NULL instead of last
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void insertEnd(struct Node** head, int val) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
}
int main() {
struct Node* head = malloc(sizeof(struct Node));
head->data = 1; head->prev = NULL;
struct Node* second = malloc(sizeof(struct Node));
second->data = 3; second->prev = head; head->next = second;
struct Node* third = malloc(sizeof(struct Node));
third->data = 4; third->prev = second; second->next = third; third->next = NULL;
insertEnd(&head, 5);
printList(head);
return 0;
}Solution
Step 1: Understand initial list and insertion
The list is 1 <-> 3 <-> 4. The function inserts 5 at the end by traversing to last node (4) and linking new node after it.Step 2: Trace printList output
After insertion, list is 1 <-> 3 <-> 4 <-> 5. Printing nodes in order outputs "1 3 4 5 "Final Answer:
1 3 4 5 -> Option BQuick Check:
List after insertEnd = 1 3 4 5 [OK]
- Not setting newNode->prev correctly
- Forgetting to set last node's next to newNode
- Printing before insertion
void insertEnd(struct Node** head, int val) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}Solution
Step 1: Check pointer assignments for doubly linked list
In doubly linked list, newNode->prev must point to the last node (temp) after insertion.Step 2: Identify missing pointer update
The code sets temp->next = newNode but does not set newNode->prev = temp, breaking backward link.Final Answer:
Missing setting newNode->prev pointer -> Option CQuick Check:
newNode->prev must be set [OK]
- Forgetting newNode->prev assignment
- Assuming singly linked list logic applies
- Not handling empty list case
Solution
Step 1: Identify last node and new node pointers
Last node is 30. New node 40 should be linked after 30 with next NULL.Step 2: Correct pointer assignments
Set last->next = newNode to link forward, newNode->prev = last for backward link, and newNode->next = NULL as it is new end.Final Answer:
last->next = newNode; newNode->prev = last; newNode->next = NULL -> Option DQuick Check:
New node linked after last with proper prev and next [OK]
- Reversing prev and next pointers
- Setting newNode->prev to NULL incorrectly
- Not setting last->next to newNode
Ready to prove it?
Pass the test and this pattern moves unknown
