Bird
Raised Fist0
DSA Goprogramming~20 mins

Insert at End of Doubly Linked List in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting nodes at the end
What is the printed state of the doubly linked list after inserting 10, 20, and 30 at the end?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    prev *Node
    next *Node
}

type DoublyLinkedList struct {
    head *Node
}

func (dll *DoublyLinkedList) InsertAtEnd(data int) {
    newNode := &Node{data: data}
    if dll.head == nil {
        dll.head = newNode
        return
    }
    temp := dll.head
    for temp.next != nil {
        temp = temp.next
    }
    temp.next = newNode
    newNode.prev = temp
}

func (dll *DoublyLinkedList) PrintList() {
    temp := dll.head
    for temp != nil {
        fmt.Print(temp.data)
        if temp.next != nil {
            fmt.Print(" <-> ")
        }
        temp = temp.next
    }
    fmt.Println(" <-> null")
}

func main() {
    dll := DoublyLinkedList{}
    dll.InsertAtEnd(10)
    dll.InsertAtEnd(20)
    dll.InsertAtEnd(30)
    dll.PrintList()
}
Anull
B30 <-> 20 <-> 10 <-> null
C10 <-> 20 <-> null
D10 <-> 20 <-> 30 <-> null
Attempts:
2 left
💡 Hint
Remember that inserting at the end adds the new node after the last node.
❓ Predict Output
intermediate
2:00remaining
Output after inserting into empty list
What is the printed state of the doubly linked list after inserting a single node with value 5 at the end?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    prev *Node
    next *Node
}

type DoublyLinkedList struct {
    head *Node
}

func (dll *DoublyLinkedList) InsertAtEnd(data int) {
    newNode := &Node{data: data}
    if dll.head == nil {
        dll.head = newNode
        return
    }
    temp := dll.head
    for temp.next != nil {
        temp = temp.next
    }
    temp.next = newNode
    newNode.prev = temp
}

func (dll *DoublyLinkedList) PrintList() {
    temp := dll.head
    for temp != nil {
        fmt.Print(temp.data)
        if temp.next != nil {
            fmt.Print(" <-> ")
        }
        temp = temp.next
    }
    fmt.Println(" <-> null")
}

func main() {
    dll := DoublyLinkedList{}
    dll.InsertAtEnd(5)
    dll.PrintList()
}
A5
Bnull
C5 <-> null
D5 <-> 5 <-> null
Attempts:
2 left
💡 Hint
Inserting into an empty list sets the head to the new node.
🔧 Debug
advanced
2:00remaining
Identify the bug in InsertAtEnd method
What error will occur when running this InsertAtEnd method if the list is empty?
DSA Go
func (dll *DoublyLinkedList) InsertAtEnd(data int) {
    newNode := &Node{data: data}
    temp := dll.head
    for temp.next != nil {
        temp = temp.next
    }
    temp.next = newNode
    newNode.prev = temp
}
Aruntime error: invalid memory address or nil pointer dereference
Bsyntax error: missing return statement
Cno error, works fine
Dcompile error: undefined variable temp
Attempts:
2 left
💡 Hint
Check what happens when dll.head is nil and you try to access temp.next.
❓ Predict Output
advanced
2:00remaining
Output after multiple InsertAtEnd calls
What is the printed state of the doubly linked list after these InsertAtEnd calls: InsertAtEnd(1), InsertAtEnd(2), InsertAtEnd(3), InsertAtEnd(4)?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    prev *Node
    next *Node
}

type DoublyLinkedList struct {
    head *Node
}

func (dll *DoublyLinkedList) InsertAtEnd(data int) {
    newNode := &Node{data: data}
    if dll.head == nil {
        dll.head = newNode
        return
    }
    temp := dll.head
    for temp.next != nil {
        temp = temp.next
    }
    temp.next = newNode
    newNode.prev = temp
}

func (dll *DoublyLinkedList) PrintList() {
    temp := dll.head
    for temp != nil {
        fmt.Print(temp.data)
        if temp.next != nil {
            fmt.Print(" <-> ")
        }
        temp = temp.next
    }
    fmt.Println(" <-> null")
}

func main() {
    dll := DoublyLinkedList{}
    dll.InsertAtEnd(1)
    dll.InsertAtEnd(2)
    dll.InsertAtEnd(3)
    dll.InsertAtEnd(4)
    dll.PrintList()
}
A1 <-> 2 <-> 3 <-> 4 <-> null
B4 <-> 3 <-> 2 <-> 1 <-> null
C1 <-> 2 <-> 3 <-> null
Dnull
Attempts:
2 left
💡 Hint
Nodes are added at the end, so order is preserved.
🧠 Conceptual
expert
2:00remaining
Why is the prev pointer important in InsertAtEnd?
In a doubly linked list, what is the main reason to set the prev pointer of the new node when inserting at the end?
ATo speed up insertion at the beginning of the list
BTo allow backward traversal from the new node to the previous node
CTo prevent memory leaks in the list
DTo automatically sort the list after insertion
Attempts:
2 left
💡 Hint
Think about what the prev pointer lets you do in a doubly linked list.

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