Bird
Raised Fist0
DSA Goprogramming~30 mins

Insert at End of Doubly Linked List in DSA Go - Build from Scratch

Choose your learning style10 modes available
Insert at End of Doubly Linked List
📖 Scenario: You are managing a playlist of songs. Each song is linked to the previous and next song, allowing easy navigation forward and backward.
🎯 Goal: Build a doubly linked list and add a new song at the end of the playlist.
📋 What You'll Learn
Create a doubly linked list node struct called Node with data, prev, and next fields
Create a variable head to represent the start of the list
Write a function insertAtEnd that adds a new node with given data at the end of the list
Print the list from head to end showing the data values
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, web browsers, and undo-redo features where you need to move forward and backward easily.
💼 Career
Understanding linked lists is fundamental for software engineers working with data structures, memory management, and building efficient algorithms.
Progress0 / 4 steps
1
Create the doubly linked list node and initial list
Create a struct called Node with fields data int, prev *Node, and next *Node. Then create a variable head of type *Node and assign it a new Node with data 10 and prev and next as nil.
DSA Go
Hint

Define the struct with the three fields. Then create head as a pointer to a new Node with data 10.

2
Create the insertAtEnd function
Create a function called insertAtEnd that takes a pointer to Node called head and an int called data. The function should return a pointer to Node. Inside, create a new node with the given data. If head is nil, return the new node. Otherwise, traverse to the last node and link the new node at the end. Return the original head.
DSA Go
Hint

Create a new node. If list is empty, return new node. Otherwise, find last node and link new node after it.

3
Insert a new node with data 20 at the end
Call the function insertAtEnd with head and 20 as arguments. Assign the result back to head.
DSA Go
Hint

Call insertAtEnd with head and 20, then update head.

4
Print the doubly linked list from head to end
Use a for loop with a variable current starting at head. Loop while current is not nil. Inside the loop, print current.data followed by " -> ". Move current to current.next. After the loop, print "nil".
DSA Go
Hint

Use a loop to print each node's data followed by ' -> '. After the loop, print 'nil'.

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: Identify the effect of inserting 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 reverses the list
  • Assuming insertion happens at the start
2. Which of the following Go code snippets correctly updates the 'next' pointer when inserting a new node at the end of a doubly linked list?
easy
A. newNode.next = last
B. newNode.prev = last.next
C. last.next = newNode
D. last.prev = newNode

Solution

  1. Step 1: Identify the last node's next pointer update

    When inserting at the end, the last node's next pointer should point to the new node.
  2. Step 2: Match correct pointer assignment

    Setting last.next = newNode correctly links the last node to the new node.
  3. Final Answer:

    last.next = newNode -> Option C
  4. Quick Check:

    last.next points to new node [OK]
Hint: Last node's next points to new node [OK]
Common Mistakes:
  • Assigning newNode.prev to last.next incorrectly
  • Setting newNode.next to last (wrong direction)
  • Changing last.prev instead of last.next
3. What will be the printed output of the doubly linked list after inserting nodes with values 10, 20, and 30 at the end in order? Assume the print function shows values from head to tail.
medium
A. null <-> 10 <-> 20 <-> 30
B. 30 <-> 20 <-> 10 <-> null
C. 10 <-> 30 <-> 20 <-> null
D. 10 <-> 20 <-> 30 <-> null

Solution

  1. Step 1: Insert 10 at end when list is empty

    List becomes: 10 -> null
  2. Step 2: Insert 20 at end

    List becomes: 10 <-> 20 -> null
  3. Step 3: Insert 30 at end

    List becomes: 10 <-> 20 <-> 30 -> null
  4. Final Answer:

    10 <-> 20 <-> 30 <-> null -> Option D
  5. Quick Check:

    Insert order preserved from head to tail [OK]
Hint: Insertion at end keeps order as inserted [OK]
Common Mistakes:
  • Assuming insertion reverses order
  • Printing null at start instead of end
  • Mixing node order in output
4. Given the following Go code snippet for inserting at the end of a doubly linked list, what is the error?
func InsertEnd(head *Node, data int) *Node {
    newNode := &Node{data: data}
    if head == nil {
        head = newNode
    } else {
        temp := head
        for temp.next != nil {
            temp = temp.next
        }
        temp.next = newNode
        newNode.prev = temp
    }
    return head
}
medium
A. The newNode's prev pointer is incorrectly assigned
B. The newNode's next pointer is not set to nil
C. The loop condition should be temp.prev != nil
D. The function does not update the head when list is empty

Solution

  1. Step 1: Check newNode pointer initialization

    In Go, uninitialized pointers default to nil, but explicitly setting newNode.next = nil is safer.
  2. Step 2: Identify missing explicit next pointer assignment

    The code does not set newNode.next = nil, which can cause issues if newNode is reused or inspected.
  3. Final Answer:

    The newNode's next pointer is not set to nil -> Option B
  4. Quick Check:

    Always set newNode.next = nil at insertion [OK]
Hint: Always set newNode.next = nil when inserting at end [OK]
Common Mistakes:
  • Ignoring explicit nil assignment for newNode.next
  • Confusing prev and next pointers
  • Incorrect loop condition for traversal
5. You have a doubly linked list with nodes 5 <-> 15 <-> 25. You want to insert a new node with value 35 at the end. After insertion, which pointers must be updated to maintain list integrity?
hard
A. Set last node's next to new node, new node's prev to last node, new node's next to nil
B. Set new node's next to last node, last node's prev to new node
C. Set head's prev to new node, new node's next to head
D. Set new node's prev and next to nil only

Solution

  1. Step 1: Identify the last node before insertion

    The last node is the one with value 25.
  2. Step 2: Update pointers to insert new node at end

    Set last node's next to new node, new node's prev to last node, and new node's next to nil to mark end.
  3. Final Answer:

    Set last node's next to new node, new node's prev to last node, new node's next to nil -> Option A
  4. Quick Check:

    Last.next = new, new.prev = last, new.next = nil [OK]
Hint: Update last.next, new.prev, and new.next for end insertion [OK]
Common Mistakes:
  • Reversing prev and next pointers
  • Not setting new node's next to nil
  • Changing head pointers incorrectly
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