A doubly linked list is a chain of nodes where each node has three parts: data, a pointer to the next node, and a pointer to the previous node. This allows moving forward and backward through the list.
Click to reveal answer
beginner
What does inserting at a specific position in a doubly linked list mean?
It means adding a new node at a chosen place in the list, not just at the start or end. We adjust the pointers of nearby nodes to include the new node.
Click to reveal answer
intermediate
What pointers need to be updated when inserting a node at position 3 in a doubly linked list?
You update the new node's next to point to the old node at position 3, its previous to point to node at position 2, then update node 2's next to new node, and node 3's previous to new node.
Click to reveal answer
beginner
Why do we need to check if the position is valid before insertion?
Because inserting at a position outside the list length can cause errors or crashes. Validating ensures the position exists or is at the end.
Click to reveal answer
beginner
What happens if you insert at position 1 in a doubly linked list?
The new node becomes the new head. Its next points to the old head, and the old head's previous points back to the new node.
Click to reveal answer
In a doubly linked list, what does each node contain?
AData, next pointer, previous pointer
BData only
CData and next pointer only
DData and previous pointer only
✗ Incorrect
Each node has data, a pointer to the next node, and a pointer to the previous node.
When inserting at position 1, what pointer of the old head node must be updated?
AIts previous pointer
BIts next pointer
CIts data
DNo pointer needs updating
✗ Incorrect
The old head's previous pointer must point to the new node inserted at position 1.
What should you do if the position to insert is greater than the list length + 1?
AInsert at the end
BReturn an error or do nothing
CInsert at the start
DInsert at position 1
✗ Incorrect
Positions beyond list length + 1 are invalid; insertion should be prevented or handled as an error.
Which pointers are updated when inserting a node between two existing nodes?
AOnly the new node's pointers
BOnly the previous node's pointer
CNew node's pointers and the adjacent nodes' pointers
DOnly the next node's pointer
✗ Incorrect
You update the new node's next and previous pointers, and also the adjacent nodes' pointers to include the new node.
What is the time complexity of inserting at a specific position in a doubly linked list?
AO(1)
BO(n^2)
CO(log n)
DO(n)
✗ Incorrect
You may need to traverse up to the position, which takes O(n) time.
Explain step-by-step how to insert a new node at position 3 in a doubly linked list.
Think about how pointers connect nodes before and after the new node.
You got /6 concepts.
What checks should you perform before inserting a node at a specific position in a doubly linked list?
Consider list boundaries and valid positions.
You got /4 concepts.
Practice
(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The new node becomes the new head of the list.
B. The new node is added at the end of the list.
C. The new node replaces the last node.
D. The list remains unchanged.
Solution
Step 1: Understand position 1 insertion
Inserting at position 1 means adding the node at the start of the list.
Step 2: Effect on the list head
The new node becomes the head, and its next points to the old head.
Final Answer:
The new node becomes the new head of the list. -> Option A
Quick Check:
Insert at position 1 = new head [OK]
Hint: Position 1 means new head insertion [OK]
Common Mistakes:
Thinking position 1 means end insertion
Not updating previous head's previous pointer
Ignoring head pointer update
2. Which of the following is the correct syntax to insert a new node at position pos in a doubly linked list in Java?
Hint: Remember to update both next and prev pointers [OK]
Common Mistakes:
Swapping next and prev pointers
Not updating current.next.prev
Incorrect order of pointer assignments
3. Given the doubly linked list: 1 <-> 2 <-> 4 <-> null, what will be the list after inserting node with value 3 at position 3?
medium
A. 1 <-> 2 <-> 3 <-> 4 <-> null
B. 1 <-> 3 <-> 2 <-> 4 <-> null
C. 3 <-> 1 <-> 2 <-> 4 <-> null
D. 1 <-> 2 <-> 4 <-> 3 <-> null
Solution
Step 1: Locate position 3 in the list
Positions: 1=1, 2=2, 3=4 currently. Insert before node with value 4.
Step 2: Insert new node with value 3 at position 3
New node's next points to node with value 4, prev points to node with value 2. Update pointers accordingly.
Final Answer:
1 <-> 2 <-> 3 <-> 4 <-> null -> Option A
Quick Check:
Insert at position 3 places 3 before 4 [OK]
Hint: Insert before node currently at position [OK]
Common Mistakes:
Inserting after position instead of at position
Not updating prev pointer of next node
Confusing node values and positions
4. Consider this code snippet to insert at position 2 in a doubly linked list:
Node newNode = new Node(5);
Node current = head;
current = current.next;
newNode.next = current.next;
newNode.prev = current;
current.next = newNode;
newNode.next.prev = newNode;
What is the error in this code?
medium
A. It does not handle insertion at head correctly.
B. It may cause NullPointerException if current.next is null.
C. It forgets to update newNode.prev pointer.
D. It inserts the node at the end always.
Solution
Step 1: Analyze newNode.next assignment
newNode.next = current.next; If current.next is null (inserting at end), newNode.next.prev causes error.
Step 2: Identify NullPointerException risk
Accessing newNode.next.prev without checking null causes NullPointerException.
Final Answer:
It may cause NullPointerException if current.next is null. -> Option B
Quick Check:
Check for null before newNode.next.prev update [OK]
Hint: Always check if next node is null before accessing its prev [OK]
Common Mistakes:
Not checking for null before pointer updates
Assuming list has more nodes without validation
Forgetting to update head or tail pointers
5. You have a doubly linked list: 10 <-> 20 <-> 30 <-> 40 <-> null. You want to insert a node with value 25 at position 3. After insertion, you also want to delete the node at position 4. What will be the final list?
hard
A. 10 <-> 25 <-> 20 <-> 40 <-> null
B. 10 <-> 20 <-> 25 <-> 30 <-> null
C. 10 <-> 20 <-> 25 <-> 40 <-> null
D. 10 <-> 20 <-> 30 <-> 25 <-> null
Solution
Step 1: Insert 25 at position 3
Positions before insertion: 1=10, 2=20, 3=30, 4=40. Insert 25 before 30, so list becomes 10 <-> 20 <-> 25 <-> 30 <-> 40.
Step 2: Delete node at position 4
After insertion, position 4 is node with value 30. Remove it by updating pointers: 25's next points to 40, 40's prev points to 25.