Bird
Raised Fist0
DSA C++programming~10 mins

Insert at End of Doubly Linked List in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at End of Doubly Linked List
Create new node with data
↓
Is list empty?
↓
Set head = new
↓
Traverse to last node
↓
Set last.next = new
↓
Set new.prev = last
↓
Done
Create a new node, check if list is empty, if yes set head to new node; else traverse to last node and link new node at end with proper prev and next pointers.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* prev;
  Node* next;
};

void insertEnd(Node*& head, int val) {
  Node* newNode = new Node{val, nullptr, nullptr};
  if (!head) head = newNode;
  else {
    Node* curr = head;
    while (curr->next) curr = curr->next;
    curr->next = newNode;
    newNode->prev = curr;
  }
}
Insert a new node with given value at the end of a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10āˆ…newNode → Node(10, prev=āˆ…, next=āˆ…)āˆ…
2Check if head is nullāˆ…head = āˆ… (empty list)āˆ…
3List empty, set head = newNodeNode(10)head → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Create new node with data=20Node(10)newNode → Node(20, prev=āˆ…, next=āˆ…)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Check if head is nullNode(10)head → Node(10) (not empty)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Traverse to last node starting at headNode(10)curr → Node(10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head newNode
7Set last.next = newNodeNode(10) → Node(20)Node(10).next → Node(20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│───→│ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
8Set newNode.prev = lastNode(10) ↔ Node(20)Node(20).prev → Node(10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│───→│ data:20│ │ prev:āˆ… │ │ prev:──┼ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
9DoneNode(10) ↔ Node(20)No pointer changesā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│───→│ data:20│ │ prev:āˆ… │ │ prev:──┼ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
šŸ’” Traversal ends when curr->next is null; new node linked at end.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6After Step 7After Step 8Final
headāˆ…āˆ…Node(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodeāˆ…Node(10)Node(10)Node(20)Node(20)Node(20)Node(20)Node(20)
currāˆ…āˆ…āˆ…āˆ…Node(10)Node(10)Node(10)Node(10)
Key Moments - 3 Insights
Why do we check if the list is empty before inserting?
Because if the list is empty (head is null), we must set head to the new node directly. This is shown in execution_table step 3 where head changes from null to the new node.
Why do we traverse to the last node before inserting?
We need to find the current last node to link its next pointer to the new node. This traversal is shown in step 6 and 7 where curr moves to last node and then curr->next is set.
Why do we set newNode.prev to the last node?
Because in a doubly linked list, each node must know its previous node. This backward link is set in step 8, ensuring the list can be traversed backwards.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the list after step 3?
AList is empty, head is null
BList has one node with data 10, head points to it
CList has two nodes linked
DNew node is not created yet
šŸ’” Hint
Check the Visual State column at step 3 in execution_table
At which step does the new node's prev pointer get set?
AStep 8
BStep 6
CStep 7
DStep 9
šŸ’” Hint
Look at Pointer Changes column for newNode.prev assignment
If the list was initially empty, which steps would be skipped?
ASteps 4, 5, 6
BSteps 1, 2, 3
CSteps 6, 7, 8
DNo steps are skipped
šŸ’” Hint
Refer to the concept_flow where traversal happens only if list is not empty
Concept Snapshot
Insert at End of Doubly Linked List:
- Create new node with data
- If list empty, set head = new node
- Else traverse to last node
- Set last.next = new node
- Set new node.prev = last node
- New node's next = null
- List size increases by 1
Full Transcript
To insert at the end of a doubly linked list, first create a new node with the given data. Check if the list is empty by seeing if head is null. If empty, set head to the new node. If not empty, start from head and traverse nodes until reaching the last node (where next is null). Then set the last node's next pointer to the new node, and set the new node's prev pointer back to the last node. The new node's next pointer is null, marking it as the new tail. This process maintains the doubly linked structure allowing traversal in both directions.

Practice

(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To add a new element after the last node
B. To remove the first node
C. To reverse the list
D. To find the middle node

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
  2. Step 2: Purpose of insertion at the end

    Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
  3. Final Answer:

    To add a new element after the last node -> Option A
  4. Quick Check:

    Insert at end = Add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insertion at end with deletion
  • Thinking insertion at end reverses the list
  • Assuming insertion happens at the start
2. Which of the following is the correct way to update the tail pointer when inserting a new node at the end of a doubly linked list in C++?
easy
A. tail = NULL;
B. tail = tail->prev;
C. tail = head;
D. tail = newNode;

Solution

  1. Step 1: Identify the new tail after insertion

    When a new node is added at the end, it becomes the new tail of the list.
  2. Step 2: Update the tail pointer

    Assign the tail pointer to point to the new node to reflect the updated end of the list.
  3. Final Answer:

    tail = newNode; -> Option D
  4. Quick Check:

    New tail points to newNode [OK]
Hint: Tail always points to the last inserted node [OK]
Common Mistakes:
  • Setting tail to previous node instead of new node
  • Resetting tail to head incorrectly
  • Assigning tail to NULL after insertion
3. What will be the output of the following code snippet after inserting 30 at the end of the doubly linked list?
struct Node {
  int data;
  Node* prev;
  Node* next;
};

Node* head = new Node{10, NULL, NULL};
Node* tail = head;

Node* newNode = new Node{30, tail, NULL};
tail->next = newNode;
tail = newNode;

// Print list from head
Node* temp = head;
while(temp != NULL) {
  std::cout << temp->data << " ";
  temp = temp->next;
}
medium
A. 10 30
B. 30 10
C. 10
D. 30

Solution

  1. Step 1: Initial list and insertion

    Initially, list has one node with data 10. New node with 30 is added at end, linked properly.
  2. Step 2: Traversing and printing

    Starting from head (10), print data then move to next node (30), print data, then NULL ends loop.
  3. Final Answer:

    10 30 -> Option A
  4. Quick Check:

    List after insertion = 10 30 [OK]
Hint: Traverse from head to see all nodes in order [OK]
Common Mistakes:
  • Printing nodes in reverse order
  • Not updating tail->next pointer
  • Forgetting to update tail pointer
4. Identify the error in the following code snippet that inserts a node at the end of a doubly linked list:
void insertEnd(Node*& head, Node*& tail, int val) {
  Node* newNode = new Node{val, NULL, NULL};
  if (head == NULL) {
    head = newNode;
    tail = newNode;
  } else {
    tail->next = newNode;
    newNode->prev = tail;
    head = newNode;  // Error here
  }
}
medium
A. head should be updated to newNode
B. newNode->prev should be NULL
C. tail should be updated to newNode
D. tail->next should be NULL

Solution

  1. Step 1: Analyze pointer updates in else block

    tail->next points to newNode and newNode->prev points to tail correctly.
  2. Step 2: Identify incorrect pointer assignment

    head is wrongly updated to newNode; it should remain unchanged. Instead, tail should be updated to newNode.
  3. Final Answer:

    tail should be updated to newNode -> Option C
  4. Quick Check:

    Tail points to last node after insertion [OK]
Hint: Update tail, not head, when inserting at end [OK]
Common Mistakes:
  • Updating head instead of tail
  • Not linking newNode's prev pointer
  • Forgetting to set tail->next
5. Given a doubly linked list with nodes 5 <-> 10 <-> 15, write the sequence of node values after inserting 20 at the end and then 2 at the end. What is the final list?
hard
A. 2 <-> 5 <-> 10 <-> 15 <-> 20
B. 5 <-> 10 <-> 15 <-> 20 <-> 2
C. 5 <-> 10 <-> 15 <-> 2 <-> 20
D. 20 <-> 2 <-> 5 <-> 10 <-> 15

Solution

  1. Step 1: Insert 20 at the end

    Starting list: 5 <-> 10 <-> 15. After inserting 20 at end: 5 <-> 10 <-> 15 <-> 20.
  2. Step 2: Insert 2 at the end

    Now add 2 at end: 5 <-> 10 <-> 15 <-> 20 <-> 2.
  3. Final Answer:

    5 <-> 10 <-> 15 <-> 20 <-> 2 -> Option B
  4. Quick Check:

    Insertions add nodes at list end in order [OK]
Hint: Insertions add nodes at the end, order preserved [OK]
Common Mistakes:
  • Inserting new nodes at the start instead of end
  • Mixing order of inserted nodes
  • Confusing next and prev pointers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard