A doubly linked list is a chain of nodes where each node has three parts: data, a link to the next node, and a link to the previous node.
Click to reveal answer
beginner
What steps are needed to insert a new node at the beginning of a doubly linked list?
1. Create a new node with the given data. 2. Set the new node's next link to the current head. 3. Set the current head's previous link to the new node (if head exists). 4. Set the new node's previous link to null. 5. Update the head to be the new node.
Click to reveal answer
intermediate
Why do we need to update the previous link of the old head node when inserting at the beginning?
Because the new node becomes the first node, the old head now comes after it, so its previous link must point back to the new node to keep the list connected both ways.
Click to reveal answer
beginner
What happens if the doubly linked list is empty when inserting at the beginning?
The new node becomes the head, and both its next and previous links are null because it is the only node in the list.
Click to reveal answer
beginner
Show the Java code snippet to insert a node at the beginning of a doubly linked list.
```java
class Node {
int data;
Node prev, next;
Node(int d) { data = d; prev = next = null; }
}
class DoublyLinkedList {
Node head;
void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
newNode.prev = null;
if (head != null) {
head.prev = newNode;
}
head = newNode;
}
}
```
Click to reveal answer
What link of the new node should be set to the current head when inserting at the beginning?
Aboth next and prev
Bprev
Cnext
Dnone
✗ Incorrect
The new node's next link points to the current head because it will come before the old first node.
When inserting at the beginning, what should be the previous link of the new node?
Anull
Bhead
Ctail
Dnew node itself
✗ Incorrect
The new node is the first node, so its previous link must be null.
If the list is empty, what will be the next link of the new node after insertion at beginning?
Ahead
Bnull
Cnew node
Dtail
✗ Incorrect
With no other nodes, the new node's next link is null.
Which pointer of the old head node must be updated when a new node is inserted at the beginning?
Aprev
Bnext
Cdata
Dhead
✗ Incorrect
The old head's prev pointer must point back to the new node.
What is the time complexity of inserting a node at the beginning of a doubly linked list?
AO(n^2)
BO(n)
CO(log n)
DO(1)
✗ Incorrect
Insertion at the beginning is done in constant time because it involves only a few pointer changes.
Explain step-by-step how to insert a new node at the beginning of a doubly linked list.
Think about how the links connect the new node and the old head.
You got /5 concepts.
Write Java code to insert a node at the beginning of a doubly linked list and explain each line.
Focus on pointer updates and head assignment.
You got /6 concepts.
Practice
(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node after the tail node
B. To reverse the entire list
C. To delete the first node of the list
D. To add a new node before the current head node
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 D
Quick Check:
Insert at beginning = add before head [OK]
Hint: Beginning insertion means new node becomes the head [OK]
Common Mistakes:
Confusing beginning with end insertion
Thinking insertion deletes nodes
Mixing up singly and doubly linked list behavior
2. Which of the following is the correct syntax to create a new node in Java for a doubly linked list?
B. Node newNode = new Node(data); newNode.prev = null; newNode.next = null;
C. Node newNode = new Node(); newNode.data = data; newNode.prev = null; newNode.next = null;
D. Node newNode = new Node(data); newNode.prev = data; newNode.next = data;
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 B
Quick Check:
Java new object = new Constructor() [OK]
Hint: Use 'new' keyword and constructor to create objects [OK]
Common Mistakes:
Omitting 'new' keyword
Assigning data to prev or next pointers
Using incorrect constructor syntax
3. What will be the output of the doubly linked list after inserting 10 at the beginning of the list containing nodes 20 <-> 30?
medium
A. 10 <-> 20 <-> 30
B. 20 <-> 30 <-> 10
C. 30 <-> 20 <-> 10
D. 10 <-> 30 <-> 20
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 A
Quick Check:
Insert at beginning adds node before head [OK]
Hint: New node at start shifts old head right [OK]
Common Mistakes:
Appending instead of prepending
Not updating previous pointer of old head
Reversing node order incorrectly
4. Given the following code snippet for inserting at the beginning of a doubly linked list, what is the error?
void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
head.prev = newNode;
head = newNode;
}
medium
A. No error, code works correctly
B. head should be updated before setting newNode.next
C. head.prev = newNode; causes NullPointerException if list is empty
D. newNode.next should be null
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 C
Quick Check:
Null head means head.prev causes error [OK]
Hint: Check for null head before accessing head.prev [OK]
Common Mistakes:
Ignoring empty list case
Assuming head is never null
Misordering pointer updates
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25. You want to insert a new node with value 1 at the beginning and then reverse the list. What will be the final list after these operations?
hard
A. 25 <-> 15 <-> 5 <-> 1
B. 1 <-> 5 <-> 15 <-> 25
C. 1 <-> 25 <-> 15 <-> 5
D. 5 <-> 15 <-> 25 <-> 1
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 A
Quick Check:
Insert at start then reverse flips order [OK]
Hint: Insert first, then reverse flips entire list [OK]
Common Mistakes:
Reversing before insertion
Not updating prev pointers when reversing
Confusing node order after reverse
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.