Create and Initialize Doubly Linked List in DSA Java - Time & Space Complexity
When we create and initialize a doubly linked list, we want to know how the time needed grows as we add more elements.
We ask: How much work does it take to build this list as it gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node prev, next;
Node(int d) { data = d; prev = next = null; }
}
class DoublyLinkedList {
Node head;
void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
}
}
This code creates a doubly linked list and adds new nodes at the end one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves to the end of the list to add a new node.
- How many times: For each add, it may traverse all existing nodes to find the end.
Each new node added requires walking through the list to find the end, so the work grows as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1+2+...+10 = 55 steps |
| 100 | About 5050 steps |
| 1000 | About 500,500 steps |
Pattern observation: The total steps grow roughly like the square of the number of nodes.
Time Complexity: O(n2)
This means adding n nodes one by one at the end takes time proportional to n squared, because each add walks through the list.
[X] Wrong: "Adding each node is always a quick constant time operation."
[OK] Correct: Without keeping track of the tail, each add must find the end by walking through the list, which takes longer as the list grows.
Understanding how adding nodes affects time helps you design better data structures and shows you can spot inefficiencies in code.
"What if we kept a reference to the tail node? How would the time complexity change when adding nodes?"
Practice
Solution
Step 1: Understand node pointers in linked lists
A singly linked list node has one pointer to the next node only.Step 2: Identify doubly linked list node pointers
A doubly linked list node has two pointers: one to the next node and one to the previous node.Final Answer:
A doubly linked list has nodes with pointers to both previous and next nodes. -> Option BQuick Check:
Doubly linked list = two pointers per node [OK]
- Confusing singly and doubly linked list pointers
- Thinking doubly linked lists use arrays
- Assuming doubly linked lists cannot go backward
Solution
Step 1: Check node fields for doubly linked list
A doubly linked list node must have data, next pointer, and previous pointer.Step 2: Match correct field names
class Node { int data; Node next; Node prev; } uses 'next' and 'prev' which are standard and consistent.Final Answer:
class Node { int data; Node next; Node prev; } -> Option AQuick Check:
Node has data, next, and prev [OK]
- Omitting prev pointer in node class
- Using inconsistent pointer names
- Confusing singly linked list node with doubly linked
class Node {
int data;
Node next;
Node prev;
Node(int d) { data = d; next = null; prev = null; }
}
public class Main {
public static void main(String[] args) {
Node head = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
head.next = second; second.prev = head;
second.next = third; third.prev = second;
Node current = head;
while(current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.print("null");
}
}Solution
Step 1: Understand list creation and linking
Nodes 10, 20, 30 are linked forward: 10.next=20, 20.next=30.Step 2: Trace the print loop
Starting at head (10), print data then move to next until null: prints 10 -> 20 -> 30 -> null.Final Answer:
10 -> 20 -> 30 -> null -> Option CQuick Check:
Forward traversal prints nodes in insertion order [OK]
- Printing in reverse order by mistake
- Missing last node in output
- Confusing prev and next pointers
class Node {
int data;
Node next;
Node prev;
Node(int d) { data = d; next = null; prev = null; }
}
public class Main {
public static void main(String[] args) {
Node head = new Node(1);
Node second = new Node(2);
head.next = second;
// Missing line here
System.out.println(head.next.data);
}
}Solution
Step 1: Check linking between nodes
head.next is assigned second, but second.prev is not set to head.Step 2: Identify missing backward link
For doubly linked list, second.prev must point back to head to maintain two-way links.Final Answer:
Missing assignment of second.prev to head -> Option AQuick Check:
Both next and prev must be linked [OK]
- Forgetting to link prev pointer
- Assuming next pointer is enough
- Ignoring bidirectional links in doubly linked list
Solution
Step 1: Create nodes from array elements
For each integer, create a new node with data set to that integer.Step 2: Link nodes forward and backward
Set current node's next to next node, and next node's prev to current node to maintain doubly linked structure.Final Answer:
Create nodes in a loop, link each node's next to the next node, and set next node's prev to current node. -> Option DQuick Check:
Proper doubly linked list needs next and prev links set correctly [OK]
- Mixing up next and prev pointers
- Not setting prev pointer at all
- Setting prev pointer incorrectly to next node
Ready to prove it?
Pass the test and this pattern moves unknown
