Bird
Raised Fist0
DSA Goprogramming~20 mins

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

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

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) deleteAtBeginning() {
    if l.head != nil {
        l.head = l.head.next
    }
}

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.head = &Node{data: 10}
    list.head.next = &Node{data: 20}
    list.head.next.next = &Node{data: 30}

    list.deleteAtBeginning()
    list.printList()
}
A20 -> 30 -> null
B10 -> 20 -> 30 -> null
C30 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the first node means the head moves to the next node.
❓ Predict Output
intermediate
2:00remaining
Output when deleting from an empty linked list
What is the output when deleteAtBeginning is called on an empty linked list?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) deleteAtBeginning() {
    if l.head != nil {
        l.head = l.head.next
    }
}

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.deleteAtBeginning()
    list.printList()
}
A0 -> null
Bpanic: runtime error
Cnull
Dempty list
Attempts:
2 left
💡 Hint
Deleting from an empty list should not cause errors.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteAtBeginning method
What error will occur when running this code that deletes the first node?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) deleteAtBeginning() {
    l.head = l.head.next
}

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.deleteAtBeginning()
    list.printList()
}
Apanic: runtime error: invalid memory address or nil pointer dereference
Bno output, program runs normally
Ccompilation error: missing return statement
Dprints null
Attempts:
2 left
💡 Hint
Check what happens if head is nil and you access head.next without checking.
❓ Predict Output
advanced
2:00remaining
Output after multiple deletions at beginning
What is the printed state of the linked list after deleting the first node twice?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) deleteAtBeginning() {
    if l.head != nil {
        l.head = l.head.next
    }
}

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.head = &Node{data: 5}
    list.head.next = &Node{data: 15}
    list.head.next.next = &Node{data: 25}

    list.deleteAtBeginning()
    list.deleteAtBeginning()
    list.printList()
}
Anull
B25 -> null
C5 -> 15 -> 25 -> null
D15 -> 25 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
🧠 Conceptual
expert
2:00remaining
Why is it important to check if the list is empty before deleting the first node?
What is the main reason to check if the linked list is empty before deleting the first node?
ATo ensure the list is sorted after deletion
BTo improve the speed of deletion operation
CTo prevent memory leaks by freeing the node manually
DTo avoid runtime errors caused by accessing a nil pointer
Attempts:
2 left
💡 Hint
Think about what happens if you try to access something that does not exist.

Practice

(1/5)
1. What happens when you delete the first node in a singly linked list?
easy
A. The head pointer moves to the second node
B. The last node is deleted
C. The list becomes empty regardless of nodes
D. The middle node is deleted

Solution

  1. Step 1: Understand the head pointer role

    The head pointer points to the first node in the list.
  2. Step 2: Effect of deleting the first node

    Deleting the first node means moving the head pointer to the next node, which becomes the new first node.
  3. Final Answer:

    The head pointer moves to the second node -> Option A
  4. Quick Check:

    Deleting first node updates head = next node [OK]
Hint: Deleting first node means head points to next node [OK]
Common Mistakes:
  • Thinking last node is deleted
  • Assuming list becomes empty always
  • Deleting middle node by mistake
2. Which of the following Go code snippets correctly deletes the first node of a singly linked list with head pointer head?
easy
A. head.next = nil
B. head.next = head
C. head = nil.next
D. head = head.next

Solution

  1. Step 1: Understand pointer update to delete first node

    To delete the first node, head must point to the second node, which is head.next.
  2. Step 2: Check each option for correctness

    head = head.next correctly assigns head = head.next. Others either assign incorrectly or cause errors.
  3. Final Answer:

    head = head.next -> Option D
  4. Quick Check:

    head = head.next deletes first node [OK]
Hint: Assign head to head.next to delete first node [OK]
Common Mistakes:
  • Assigning head.next to head (wrong direction)
  • Using nil.next which causes error
  • Setting head.next to nil deletes wrong node
3. Given the following Go code for a singly linked list, what will be printed after deleting the first node?
package main
import "fmt"

type Node struct {
    value int
    next  *Node
}

func main() {
    head := &Node{value: 1}
    head.next = &Node{value: 2}
    head.next.next = &Node{value: 3}

    // Delete first node
    head = head.next

    // Print list
    current := head
    for current != nil {
        fmt.Print(current.value, " -> ")
        current = current.next
    }
    fmt.Print("null")
}
medium
A. 2 -> 3 -> null
B. 1 -> 2 -> 3 -> null
C. 3 -> null
D. null

Solution

  1. Step 1: Initial list nodes and values

    The list starts as 1 -> 2 -> 3 -> null.
  2. Step 2: Deleting first node by head = head.next

    Head moves from node with value 1 to node with value 2, so list becomes 2 -> 3 -> null.
  3. Final Answer:

    2 -> 3 -> null -> Option A
  4. Quick Check:

    After deletion, list starts at second node [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
  • Printing original list without deletion
  • Assuming first node still prints
  • Confusing null with empty list
4. The following Go code attempts to delete the first node of a singly linked list but causes a runtime error. What is the error and how to fix it?
package main

type Node struct {
    value int
    next  *Node
}

func main() {
    var head *Node = nil

    // Delete first node
    head = head.next
}
medium
A. Compile error: cannot assign head.next to head
B. Runtime panic: nil pointer dereference; fix by checking if head is nil before deletion
C. No error; code works fine
D. Syntax error: missing import; fix by adding fmt package

Solution

  1. Step 1: Identify cause of runtime panic

    head is nil, so accessing head.next causes nil pointer dereference panic.
  2. Step 2: Fix by checking if head is nil before deleting

    Check if head != nil before assigning head = head.next to avoid panic.
  3. Final Answer:

    Runtime panic: nil pointer dereference; fix by checking if head is nil before deletion -> Option B
  4. Quick Check:

    Nil check prevents runtime panic [OK]
Hint: Always check head != nil before deleting first node [OK]
Common Mistakes:
  • Ignoring nil check causing panic
  • Assuming head is never nil
  • Confusing syntax and runtime errors
5. You have a singly linked list with nodes: 10 -> 20 -> 30 -> 40 -> null. Write a Go function to delete the first node and return the new head. What will be the list after deletion?
func deleteFirstNode(head *Node) *Node {
    if head == nil {
        return nil
    }
    head = head.next
    return head
}
hard
A. 30 -> 40 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 20 -> 30 -> 40 -> null
D. null

Solution

  1. Step 1: Understand function logic

    If head is nil, return nil. Otherwise, move head to head.next and return it.
  2. Step 2: Apply function to list 10 -> 20 -> 30 -> 40 -> null

    Deleting first node (10) moves head to node with value 20, so list becomes 20 -> 30 -> 40 -> null.
  3. Final Answer:

    20 -> 30 -> 40 -> null -> Option C
  4. Quick Check:

    Function returns list starting from second node [OK]
Hint: Return head.next to delete first node and update list [OK]
Common Mistakes:
  • Returning original head without deletion
  • Not handling nil head case
  • Deleting wrong node by mistake
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