What if you could add new items to the start of a list instantly without breaking anything?
Why Insert at Beginning of Doubly Linked List in DSA C++?
Imagine you have a long chain of paper clips linked together. Now, you want to add a new paper clip right at the start of the chain. Doing this by hand means unhooking the first clip and carefully attaching the new one before it, making sure nothing falls apart.
Manually adding a new clip at the start can be tricky and slow. If you don't hold the clips correctly, the chain might break or get tangled. Also, if the chain is very long, finding the start and adjusting links without a clear method can cause mistakes and waste time.
Using a doubly linked list lets you add a new element at the beginning easily. It keeps track of both the previous and next links, so you can quickly attach the new node at the start without breaking the chain. This method is fast and safe, even for long lists.
Node* new_node = new Node(data);
new_node->next = head;
// Manually fix previous pointers and head
head = new_node;void insertAtBeginning(int data) {
Node* new_node = new Node(data);
new_node->prev = nullptr;
new_node->next = head;
if (head != nullptr) {
head->prev = new_node;
}
head = new_node;
}This operation makes it possible to quickly add new items at the start of a list, enabling efficient updates and easy management of data sequences.
Think of a music playlist where you want to add a new favorite song right at the top to play next. Using this method, you can insert the song instantly without rearranging the whole list.
Manual insertion at the start is slow and error-prone.
Doubly linked lists track both directions for easy insertion.
Inserting at the beginning is fast and keeps the list intact.
Practice
Solution
Step 1: Understand the head node position
The head node is the first node in the doubly linked list.Step 2: Insert new node before head
Inserting at the beginning means placing the new node before the current head and updating pointers.Final Answer:
To add a new node before the current head node -> Option BQuick Check:
Insert at beginning = Add before head [OK]
- Confusing insertion at beginning with insertion at end
- Not updating the previous head's previous pointer
- Trying to delete instead of insert
Solution
Step 1: Create new node with pointer
Use pointer syntax: Node* newNode = new Node(data);Step 2: Update pointers for doubly linked list
Set newNode->next to current head, update head->prev to newNode, then update head to newNode.Final Answer:
Node* newNode = new Node(data); head->prev = newNode; newNode->next = head; head = newNode; -> Option AQuick Check:
Correct pointer updates = Node* newNode = new Node(data); head->prev = newNode; newNode->next = head; head = newNode; [OK]
- Using object instead of pointer for new node
- Not updating head->prev pointer
- Swapping next and prev pointers incorrectly
struct Node {
int data;
Node* prev;
Node* next;
Node(int d) : data(d), prev(nullptr), next(nullptr) {}
};
Node* head = new Node(20);
head->next = new Node(30);
head->next->prev = head;
Node* newNode = new Node(10);
newNode->next = head;
head->prev = newNode;
head = newNode;
// Print list from head
Node* temp = head;
while(temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null";
Solution
Step 1: Initial list setup
List initially: 20 <-> 30, head points to 20.Step 2: Insert 10 at beginning
New node 10 points next to 20, 20's prev points to 10, head updated to 10.Step 3: Print list from head
Traverse from head: 10 -> 20 -> 30 -> nullFinal Answer:
10 -> 20 -> 30 -> null -> Option CQuick Check:
New head is 10, followed by 20 and 30 [OK]
- Not updating head pointer after insertion
- Incorrect pointer assignments causing wrong order
- Printing from tail instead of head
void insertAtBeginning(Node*& head, int data) {
Node* newNode = new Node(data);
newNode->next = head;
head->prev = newNode;
head = newNode;
}Solution
Step 1: Analyze pointer usage
Code accesses head->prev without checking if head is nullptr (empty list).Step 2: Identify potential runtime error
If head is nullptr, accessing head->prev causes a crash (null pointer dereference).Final Answer:
Missing check if head is nullptr before accessing head->prev -> Option DQuick Check:
Always check head != nullptr before head->prev [OK]
- Ignoring nullptr checks causing crashes
- Incorrect pointer assignments for prev
- Passing head by value instead of reference
Solution
Step 1: Initial list
List is 5 -> 15 -> 25 -> null.Step 2: Insert 1 at beginning
New head is 1, list becomes 1 -> 5 -> 15 -> 25 -> null.Step 3: Insert 35 at beginning
New head is 35, list becomes 35 -> 1 -> 5 -> 15 -> 25 -> null.Final Answer:
35 -> 1 -> 5 -> 15 -> 25 -> null -> Option AQuick Check:
Last inserted node is new head [OK]
- Inserting nodes at end instead of beginning
- Not updating head pointer after each insertion
- Mixing order of inserted nodes
Ready to prove it?
Pass the test and this pattern moves unknown
