Bird
Raised Fist0
DSA Goprogramming~20 mins

Delete Node at End in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node from a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(data int) {
    newNode := &Node{data: data}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteEnd() {
    if l.head == nil {
        return
    }
    if l.head.next == nil {
        l.head = nil
        return
    }
    current := l.head
    for current.next.next != nil {
        current = current.next
    }
    current.next = nil
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.data)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(10)
    list.append(20)
    list.append(30)
    list.deleteEnd()
    list.printList()
}
A10 -> 20 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Think about what happens to the last node when you delete it.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node from a single-node linked list
What is the printed state of the linked list after deleting the last node when the list has only one node?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(data int) {
    newNode := &Node{data: data}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteEnd() {
    if l.head == nil {
        return
    }
    if l.head.next == nil {
        l.head = nil
        return
    }
    current := l.head
    for current.next.next != nil {
        current = current.next
    }
    current.next = nil
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.data)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(100)
    list.deleteEnd()
    list.printList()
}
AError: nil pointer dereference
B100 -> null
Cnull
D100
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteEnd method for empty list
What error will occur if deleteEnd is called on an empty linked list with the following code?
DSA Go
func (l *LinkedList) deleteEnd() {
    current := l.head
    for current.next.next != nil {
        current = current.next
    }
    current.next = nil
}
Acompilation error: missing return statement
Bruntime error: invalid memory address or nil pointer dereference
Cno error, deletes last node correctly
Druntime error: index out of range
Attempts:
2 left
💡 Hint
Consider what happens when l.head is nil.
❓ Predict Output
advanced
2:00remaining
Output after multiple deletions at end
What is the printed state of the linked list after deleting the last node twice in a list with nodes 5 -> 15 -> 25 -> 35?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) append(data int) {
    newNode := &Node{data: data}
    if l.head == nil {
        l.head = newNode
        return
    }
    current := l.head
    for current.next != nil {
        current = current.next
    }
    current.next = newNode
}

func (l *LinkedList) deleteEnd() {
    if l.head == nil {
        return
    }
    if l.head.next == nil {
        l.head = nil
        return
    }
    current := l.head
    for current.next.next != nil {
        current = current.next
    }
    current.next = nil
}

func (l *LinkedList) printList() {
    current := l.head
    for current != nil {
        fmt.Printf("%d -> ", current.data)
        current = current.next
    }
    fmt.Println("null")
}

func main() {
    list := LinkedList{}
    list.append(5)
    list.append(15)
    list.append(25)
    list.append(35)
    list.deleteEnd()
    list.deleteEnd()
    list.printList()
}
A15 -> 25 -> null
B5 -> 15 -> 25 -> null
C5 -> 15 -> 25 -> 35 -> null
D5 -> 15 -> null
Attempts:
2 left
💡 Hint
Each deleteEnd removes the last node.
🧠 Conceptual
expert
2:00remaining
Why is it necessary to traverse to the second last node to delete the last node in a singly linked list?
Choose the best explanation for why deleting the last node in a singly linked list requires traversal to the second last node.
ABecause the singly linked list nodes do not have a pointer to their previous node, so we must find the node before the last to update its next pointer to nil.
BBecause the singly linked list stores nodes in reverse order, so the second last node is actually the first node.
CBecause the last node's data must be copied to the second last node before deletion.
DBecause the last node is always the head node in a singly linked list.
Attempts:
2 left
💡 Hint
Think about how nodes are connected in a singly linked list.

Practice

(1/5)
1. What does the operation Delete Node at End do in a linked list?
easy
A. Removes the last node from the linked list
B. Adds a new node at the end of the linked list
C. Removes the first node from the linked list
D. Deletes all nodes in the linked list

Solution

  1. Step 1: Understand the linked list structure

    A linked list is a chain of nodes where each node points to the next node.
  2. Step 2: Identify the effect of deleting the last node

    Deleting the last node means removing the node that has no next node, effectively shortening the list by one at the end.
  3. Final Answer:

    Removes the last node from the linked list -> Option A
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Deleting at end always removes the tail node [OK]
Common Mistakes:
  • Confusing delete at end with delete at start
  • Thinking it adds a node instead of removing
  • Assuming it deletes all nodes
2. Which of the following Go code snippets correctly deletes the last node of a singly linked list?
easy
A. func deleteEnd(head *Node) *Node { if head == nil || head.next == nil { return nil } curr := head; for curr.next.next != nil { curr = curr.next } curr.next = nil; return head }
B. func deleteEnd(head *Node) *Node { head = head.next; return head }
C. func deleteEnd(head *Node) *Node { head = nil; return head }
D. func deleteEnd(head *Node) *Node { curr := head; for curr.next != nil { curr = curr.next } curr = nil; return head }

Solution

  1. Step 1: Check for empty or single node list

    If list is empty or has one node, deleting last node results in empty list (return nil).
  2. Step 2: Traverse to second last node and remove last

    Loop until curr.next.next is nil, then set curr.next to nil to remove last node.
  3. Final Answer:

    Code in option A correctly deletes the last node -> Option A
  4. Quick Check:

    Traverse to second last node, set next to nil [OK]
Hint: Stop at second last node, then remove last [OK]
Common Mistakes:
  • Setting head to nil incorrectly
  • Not stopping at second last node
  • Trying to delete node by setting local pointer nil
3. What will be the output linked list after running this Go code to delete the last node?
type Node struct { val int; next *Node }
func main() {
head := &Node{val: 1, next: &Node{val: 2, next: &Node{val: 3, next: nil}}}
head = deleteEnd(head)
printList(head)
}
func deleteEnd(head *Node) *Node {
if head == nil || head.next == nil { return nil }
curr := head
for curr.next.next != nil { curr = curr.next }
curr.next = nil
return head
}
func printList(head *Node) {
for head != nil {
fmt.Print(head.val, " -> ")
head = head.next
}
fmt.Println("nil")
}
medium
A. 2 -> 3 -> nil
B. 1 -> 2 -> 3 -> nil
C. 1 -> 2 -> nil
D. nil

Solution

  1. Step 1: Understand initial list

    List is 1 -> 2 -> 3 -> nil before deletion.
  2. Step 2: Delete last node

    deleteEnd removes node with value 3, so list becomes 1 -> 2 -> nil.
  3. Final Answer:

    1 -> 2 -> nil -> Option C
  4. Quick Check:

    Last node removed, list ends at 2 [OK]
Hint: Deleting end removes last value from printed list [OK]
Common Mistakes:
  • Forgetting to update next pointer to nil
  • Deleting first node instead of last
  • Printing old list without deletion
4. Find the bug in this Go function that tries to delete the last node of a singly linked list:
func deleteEnd(head *Node) *Node {
if head == nil { return nil }
curr := head
for curr.next != nil {
curr = curr.next
}
curr = nil
return head
}
medium
A. It does not handle empty list correctly
B. It causes a runtime panic due to nil pointer dereference
C. It deletes the entire list instead of last node
D. It does not update the second last node's next pointer

Solution

  1. Step 1: Analyze the loop

    The loop moves curr to the last node (where curr.next is nil).
  2. Step 2: Check deletion logic

    Setting curr = nil only changes local pointer, does not update previous node's next pointer, so last node remains linked.
  3. Final Answer:

    It does not update the second last node's next pointer -> Option D
  4. Quick Check:

    Must update second last node's next to nil [OK]
Hint: Update second last node's next, not local pointer [OK]
Common Mistakes:
  • Setting local pointer nil instead of previous node's next
  • Not handling single node list
  • Assuming curr = nil deletes node
5. Given a singly linked list, how would you modify the deleteEnd function to handle the edge case when the list has only one node?
hard
A. Do nothing and return head as is
B. Return nil immediately if head.next is nil, indicating list becomes empty
C. Set head.next to nil and return head
D. Traverse the list twice to confirm single node

Solution

  1. Step 1: Identify single node case

    If head.next is nil, list has only one node.
  2. Step 2: Decide deletion result

    Deleting last node means list becomes empty, so return nil.
  3. Final Answer:

    Return nil immediately if head.next is nil, indicating list becomes empty -> Option B
  4. Quick Check:

    Single node deletion returns empty list [OK]
Hint: Check if only one node, then return nil [OK]
Common Mistakes:
  • Not handling single node case causing errors
  • Setting head.next to nil but returning non-empty list
  • Unnecessary extra traversal
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