Doubly Linked List Structure and Node Design in DSA Java - Time & Space Complexity
We want to understand how the time to perform operations on a doubly linked list grows as the list gets bigger.
Specifically, we look at how the design of nodes and the list structure affects the speed of common actions.
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
This code defines a node for a doubly linked list with links to both previous and next nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing nodes by following next or prev links.
- How many times: Up to once per node, depending on the operation.
When we move through the list, the time grows with the number of nodes we visit.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
Pattern observation: The time grows linearly as the list size increases.
Time Complexity: O(n)
This means the time to traverse or find a node grows directly with the number of nodes in the list.
[X] Wrong: "Accessing any node in a doubly linked list is instant like an array."
[OK] Correct: Unlike arrays, linked lists require stepping through nodes one by one, so access time depends on position.
Understanding how node design affects traversal time helps you explain linked list operations clearly and confidently in interviews.
"What if we added a direct pointer to the middle node? How would that change the time complexity for accessing nodes?"
Practice
Solution
Step 1: Understand node pointers in singly linked list
A singly linked list node has only one pointer to the next node.Step 2: Understand node pointers in doubly linked list
A doubly linked list node has two pointers: one to the previous node and one to the next node.Final Answer:
A doubly linked list node has pointers to both the previous and next nodes. -> Option DQuick Check:
Doubly linked list nodes have two pointers = D [OK]
- Thinking doubly linked list nodes only point forward
- Confusing data storage with pointer structure
- Assuming doubly linked list nodes have no data
Solution
Step 1: Check for both previous and next pointers
A doubly linked list node must have pointers named prev and next to link both directions.Step 2: Verify data type and pointer names
class Node { int data; Node prev; Node next; } declares int data and Node prev and Node next, which matches the doubly linked list node structure.Final Answer:
class Node { int data; Node prev; Node next; } -> Option CQuick Check:
Node with prev and next pointers = C [OK]
- Missing prev pointer in node class
- Using incorrect pointer names like left/right
- Declaring only one pointer for next node
class Node {
int data;
Node prev, next;
Node(int d) { data = d; prev = next = null; }
}
public class TestDLL {
public static void main(String[] args) {
Node head = new Node(10);
Node second = new Node(20);
head.next = second;
second.prev = head;
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
}Solution
Step 1: Understand node linking
Node head points to 10, second node is 20. head.next = second and second.prev = head links them correctly.Step 2: Trace the while loop printing
Starting at head (10), print 10, move to next (20), print 20, then next is null, loop ends.Final Answer:
10 20 -> Option AQuick Check:
Print from head to tail = 10 20 [OK]
- Printing in reverse order by mistake
- Forgetting to link prev pointer
- Assuming output includes null or extra spaces
Node newNode = new Node(30); newNode.next = head; head.prev = newNode; head = newNode;
Solution
Step 1: Analyze pointer assignments
newNode.next = head sets newNode's next to current head. Then head.prev = newNode sets head's prev to newNode.Step 2: Check for null head case
If head is null (empty list), head.prev causes NullPointerException. Need to check if head != null before assigning head.prev.Final Answer:
head.prev = newNode; causes NullPointerException if head is null; fix by checking if head != null before assignment. -> Option AQuick Check:
Check for null before head.prev assignment = B [OK]
- Ignoring null check on head before setting prev
- Setting newNode.next to null incorrectly
- Changing head before linking nodes
Solution
Step 1: Set newNode pointers to link between nodeA and nodeB
newNode.prev must point to nodeA and newNode.next must point to nodeB to place it between them.Step 2: Update nodeA and nodeB pointers to include newNode
nodeA.next should point to newNode and nodeB.prev should point to newNode to complete the insertion.Final Answer:
newNode.prev = nodeA; newNode.next = nodeB; nodeA.next = newNode; nodeB.prev = newNode; -> Option BQuick Check:
Link newNode between nodeA and nodeB correctly = A [OK]
- Swapping prev and next pointers incorrectly
- Updating neighbors before setting newNode pointers
- Not updating both nodeA.next and nodeB.prev
Ready to prove it?
Pass the test and this pattern moves unknown
