Create and Initialize Doubly Linked List in DSA C++ - Time & Space Complexity
We want to understand how long it takes to create and set up a doubly linked list as it grows.
How does the time needed change when we add more nodes?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
Node* prev;
Node* next;
};
Node* createList(int n) {
if (n == 0) return nullptr;
Node* head = new Node{1, nullptr, nullptr};
Node* current = head;
for (int i = 2; i <= n; i++) {
Node* newNode = new Node{i, current, nullptr};
current->next = newNode;
current = newNode;
}
return head;
}
This code creates a doubly linked list with n nodes, linking each node to the previous and next.
- Primary operation: The for-loop that creates and links each new node.
- How many times: It runs (n - 1) times, once for each node after the first.
Each new node requires a fixed number of steps to create and link.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 node creations and linkings |
| 100 | About 99 node creations and linkings |
| 1000 | About 999 node creations and linkings |
Pattern observation: The work grows directly with the number of nodes; doubling nodes doubles the work.
Time Complexity: O(n)
This means the time to create the list grows in a straight line with the number of nodes.
[X] Wrong: "Creating a doubly linked list takes constant time regardless of size."
[OK] Correct: Each node must be created and linked, so more nodes mean more work, not the same.
Understanding this helps you explain how data structures grow and why efficient creation matters in real projects.
"What if we added a nested loop to link nodes in both directions after creation? How would the time complexity change?"
Practice
Solution
Step 1: Understand doubly linked list structure
A doubly linked list has nodes with pointers to both previous and next nodes.Step 2: Identify traversal capability
This structure allows moving forward and backward through the list easily.Final Answer:
To allow traversal in both forward and backward directions -> Option AQuick Check:
Doubly linked list = forward and backward traversal [OK]
- Confusing doubly linked list with arrays
- Thinking it sorts data automatically
- Assuming it only allows forward traversal
Solution
Step 1: Check pointer usage in node structure
Each node must have pointers to next and previous nodes, so use Node* for both.Step 2: Verify syntax correctness
struct Node { int data; Node* next; Node* prev; }; correctly uses pointers (Node*) for next and prev, while others use incorrect types.Final Answer:
struct Node { int data; Node* next; Node* prev; }; -> Option DQuick Check:
Node pointers use * for next and prev [OK]
- Using Node instead of Node* for pointers
- Missing prev pointer in node
- Incorrect order of members
struct Node {
int data;
Node* next;
Node* prev;
};
int main() {
Node* head = new Node{10, nullptr, nullptr};
Node* second = new Node{20, nullptr, head};
head->next = second;
std::cout << head->data << " -> " << second->data << " -> null";
return 0;
}Solution
Step 1: Analyze node creation and linking
First node has data 10, second node has data 20 and points back to first node. Head's next points to second.Step 2: Trace output statement
Prints head data (10), then second data (20), then "-> null" string.Final Answer:
10 -> 20 -> null -> Option BQuick Check:
Print order matches linked list nodes [OK]
- Confusing prev and next pointers
- Printing second node first
- Forgetting to set head->next
struct Node {
int data;
Node* next;
Node* prev;
};
int main() {
Node* head = new Node;
head->data = 5;
head->next = head->prev = nullptr;
head->prev = head;
return 0;
}Solution
Step 1: Review pointer assignments
head->next and head->prev are set to nullptr, then head->prev is set to head itself.Step 2: Identify logical error
head->prev pointing to head creates a circular reference which is incorrect for a single node list.Final Answer:
head->prev should not point to head itself -> Option AQuick Check:
Single node prev = nullptr, not self [OK]
- Setting prev or next to self causing loops
- Forgetting to initialize data
- Confusing next and prev pointers
Solution
Step 1: Check node creation and pointer assignments
Node* first = new Node{1, nullptr, nullptr}; Node* second = new Node{2, nullptr, first}; first->next = second; Node* third = new Node{3, nullptr, second}; second->next = third; creates nodes with correct data and sets next and prev pointers properly: second's prev points to first, first's next points to second, third's prev points to second, second's next points to third.Step 2: Verify linkage correctness
All nodes are linked forward and backward correctly, forming a proper doubly linked list.Final Answer:
Node* first = new Node{1, nullptr, nullptr}; Node* second = new Node{2, nullptr, first}; first->next = second; Node* third = new Node{3, nullptr, second}; second->next = third; -> Option CQuick Check:
Next and prev pointers set correctly in Node* first = new Node{1, nullptr, nullptr}; Node* second = new Node{2, nullptr, first}; first->next = second; Node* third = new Node{3, nullptr, second}; second->next = third; [OK]
- Swapping next and prev pointers
- Not linking nodes both ways
- Setting prev or next to nullptr incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
