Doubly Linked List Structure and Node Design in DSA C# - Time & Space Complexity
We want to understand how the time to create and link nodes in a doubly linked list grows as we add more nodes.
How does the work change when the list gets bigger?
Analyze the time complexity of the following code snippet.
public class Node {
public int Value;
public Node Prev;
public Node Next;
public Node(int value) {
Value = value;
Prev = null;
Next = null;
}
}
This code defines a node for a doubly linked list with pointers to previous and next nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a single node and setting its pointers.
- How many times: Each node creation is a single operation; no loops or recursion here.
Each node creation takes the same small amount of work, so the total work grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 node creations |
| 100 | 100 node creations |
| 1000 | 1000 node creations |
Pattern observation: The work grows in a straight line as we add more nodes.
Time Complexity: O(1) per node creation
This means creating each node takes a fixed amount of time, no matter how many nodes exist.
[X] Wrong: "Creating a node takes longer as the list grows because it has to check all other nodes."
[OK] Correct: Creating a node only sets its own pointers; it does not scan or loop through the list.
Understanding the basic cost of node creation helps you build efficient linked list operations and shows you know how data structures work under the hood.
"What if we added a loop to link multiple nodes at once? How would the time complexity change?"
Practice
Doubly Linked List compared to a singly linked list?Solution
Step 1: Understand the structure difference
A doubly linked list has nodes with two pointers: one to the next node and one to the previous node.Step 2: Identify traversal capabilities
This allows moving forward and backward through the list, unlike singly linked lists which only move forward.Final Answer:
It allows traversal in both forward and backward directions. -> Option AQuick Check:
Doubly linked list = two-way traversal [OK]
- Thinking it uses less memory than singly linked list
- Assuming it doesn't need a head pointer
- Believing it sorts elements automatically
Solution
Step 1: Check for public access and all pointers
The node must have public fields for data, next, and prev to represent doubly linked list nodes.Step 2: Verify syntax correctness
public class Node { public int data; public Node next; public Node prev; } declares all required fields with public access, suitable for a doubly linked list node.Final Answer:
public class Node { public int data; public Node next; public Node prev; } -> Option BQuick Check:
Node class with data, next, prev = public class Node { public int data; public Node next; public Node prev; } [OK]
- Omitting the prev pointer
- Using struct instead of class (less common for linked lists)
- Not making fields public for access
Node node = new Node(); node.data = 10; node.next = null; node.prev = null; Console.WriteLine(node.data);
Solution
Step 1: Analyze node initialization
The node's data is set to 10 explicitly, and next and prev are set to null.Step 2: Check the printed value
The Console.WriteLine prints node.data, which is 10.Final Answer:
10 -> Option AQuick Check:
node.data = 10 prints 10 [OK]
- Confusing null pointers with data value
- Expecting default 0 instead of assigned 10
- Thinking it causes compilation error
Node newNode = new Node(); newNode.data = 5; newNode.next = head; head.prev = newNode; head = newNode;
Solution
Step 1: Understand insertion at list start
newNode's next points to current head, head's prev points back to newNode, then head updates to newNode.Step 2: Check pointer assignments
newNode.prev is null by default or not needed explicitly here; the code correctly links nodes.Final Answer:
No error, code correctly inserts at the beginning -> Option CQuick Check:
Insertion at head with correct pointers = No error, code correctly inserts at the beginning [OK]
- Assuming prev must be set explicitly to null
- Thinking head.prev must be set after head update
- Confusing next pointer assignment
Solution
Step 1: Identify nodes around the removed node
To remove a node, you must link its previous node to its next node directly.Step 2: Update pointers to bypass the removed node
Set previous node's next to removed node's next, and next node's prev to removed node's prev.Final Answer:
The previous node's next and the next node's prev pointers -> Option DQuick Check:
Update prev.next and next.prev to remove node [OK]
- Only updating one pointer and breaking the list
- Trying to update head or tail unnecessarily
- Ignoring the prev pointer of the next node
Ready to prove it?
Pass the test and this pattern moves unknown
