Insert at Beginning of Doubly Linked List in DSA Java - Time & Space Complexity
We want to understand how the time needed to insert a new node at the start of a doubly linked list changes as the list grows.
Specifically, how does the number of steps change when the list gets longer?
Analyze the time complexity of the following code snippet.
class Node {
int data;
Node prev, next;
Node(int d) { data = d; prev = next = null; }
}
Node insertAtBeginning(Node head, int data) {
Node newNode = new Node(data);
newNode.next = head;
if (head != null) head.prev = newNode;
newNode.prev = null;
return newNode;
}
This code creates a new node and inserts it at the start of the doubly linked list.
Look for loops or repeated steps that depend on list size.
- Primary operation: Creating and linking one new node.
- How many times: Only once per insertion, no loops or traversals.
Adding a node at the start always takes the same number of steps, no matter how long the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 steps (create node, link pointers) |
| 100 | 5 steps (same as above) |
| 1000 | 5 steps (still the same) |
Pattern observation: The time stays constant regardless of list size.
Time Complexity: O(1)
This means inserting at the beginning takes a fixed amount of time, no matter how big the list is.
[X] Wrong: "Inserting at the start takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change a few pointers at the start; we never move or touch the other nodes.
Knowing this helps you explain why linked lists are good for quick insertions at the start, a common question in interviews.
"What if we had to insert at the end of the doubly linked list without a tail pointer? How would the time complexity change?"
Practice
Solution
Step 1: Understand the position of insertion
Inserting at the beginning means placing the new node before the current head node.Step 2: Identify the effect on the list
The new node becomes the new head, and its next pointer points to the old head.Final Answer:
To add a new node before the current head node -> Option DQuick Check:
Insert at beginning = add before head [OK]
- Confusing beginning with end insertion
- Thinking insertion deletes nodes
- Mixing up singly and doubly linked list behavior
Solution
Step 1: Understand Java object creation syntax
In Java, objects are created using the 'new' keyword followed by the constructor.Step 2: Check node initialization
The node's previous and next pointers should be set to null initially.Final Answer:
Node newNode = new Node(data); newNode.prev = null; newNode.next = null; -> Option BQuick Check:
Java new object = new Constructor() [OK]
- Omitting 'new' keyword
- Assigning data to prev or next pointers
- Using incorrect constructor syntax
Solution
Step 1: Identify current list order
The list currently is 20 <-> 30, with 20 as head and 30 as tail.Step 2: Insert 10 at beginning
New node 10 becomes head, its next points to 20, and 20's prev points to 10.Final Answer:
10 <-> 20 <-> 30 -> Option AQuick Check:
Insert at beginning adds node before head [OK]
- Appending instead of prepending
- Not updating previous pointer of old head
- Reversing node order incorrectly
void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
head.prev = newNode;
head = newNode;
}Solution
Step 1: Analyze code for empty list case
If the list is empty, head is null, so head.prev causes NullPointerException.Step 2: Identify fix
Check if head is not null before setting head.prev = newNode.Final Answer:
head.prev = newNode; causes NullPointerException if list is empty -> Option CQuick Check:
Null head means head.prev causes error [OK]
- Ignoring empty list case
- Assuming head is never null
- Misordering pointer updates
Solution
Step 1: Insert 1 at beginning
List becomes 1 <-> 5 <-> 15 <-> 25 after insertion.Step 2: Reverse the list
Reversing swaps head and tail, so list becomes 25 <-> 15 <-> 5 <-> 1.Final Answer:
25 <-> 15 <-> 5 <-> 1 -> Option AQuick Check:
Insert at start then reverse flips order [OK]
- Reversing before insertion
- Not updating prev pointers when reversing
- Confusing node order after reverse
Ready to prove it?
Pass the test and this pattern moves unknown
