Bird
Raised Fist0
DSA Goprogramming~20 mins

Delete Node at Specific Position in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting node at position 2
What is the printed linked list after deleting the node at position 2 (0-based index)?
DSA Go
package main
import "fmt"
type Node struct {
    data int
    next *Node
}
func deleteAtPosition(head *Node, pos int) *Node {
    if head == nil {
        return nil
    }
    if pos == 0 {
        return head.next
    }
    current := head
    for i := 0; current != nil && i < pos-1; i++ {
        current = current.next
    }
    if current == nil || current.next == nil {
        return head
    }
    current.next = current.next.next
    return head
}
func printList(head *Node) {
    for head != nil {
        fmt.Printf("%d -> ", head.data)
        head = head.next
    }
    fmt.Println("null")
}
func main() {
    head := &Node{data: 1}
    head.next = &Node{data: 2}
    head.next.next = &Node{data: 3}
    head.next.next.next = &Node{data: 4}
    head = deleteAtPosition(head, 2)
    printList(head)
}
A1 -> 2 -> 3 -> null
B2 -> 3 -> 4 -> null
C1 -> 2 -> 4 -> null
D1 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that position 2 means the third node in the list.
❓ Predict Output
intermediate
2:00remaining
Output after deleting node at position 0
What is the printed linked list after deleting the node at position 0 (head node)?
DSA Go
package main
import "fmt"
type Node struct {
    data int
    next *Node
}
func deleteAtPosition(head *Node, pos int) *Node {
    if head == nil {
        return nil
    }
    if pos == 0 {
        return head.next
    }
    current := head
    for i := 0; current != nil && i < pos-1; i++ {
        current = current.next
    }
    if current == nil || current.next == nil {
        return head
    }
    current.next = current.next.next
    return head
}
func printList(head *Node) {
    for head != nil {
        fmt.Printf("%d -> ", head.data)
        head = head.next
    }
    fmt.Println("null")
}
func main() {
    head := &Node{data: 10}
    head.next = &Node{data: 20}
    head.next.next = &Node{data: 30}
    head = deleteAtPosition(head, 0)
    printList(head)
}
A30 -> null
B10 -> 20 -> 30 -> null
Cnull
D20 -> 30 -> null
Attempts:
2 left
💡 Hint
Deleting position 0 removes the head node.
🔧 Debug
advanced
2:00remaining
Identify the error in deletion at position function
What error will this code produce when trying to delete a node at position 3 in a list of length 3?
DSA Go
package main
import "fmt"
type Node struct {
    data int
    next *Node
}
func deleteAtPosition(head *Node, pos int) *Node {
    if head == nil {
        return nil
    }
    if pos == 0 {
        return head.next
    }
    current := head
    for i := 0; i < pos-1; i++ {
        current = current.next
    }
    current.next = current.next.next
    return head
}
func main() {
    head := &Node{data: 5}
    head.next = &Node{data: 10}
    head.next.next = &Node{data: 15}
    head = deleteAtPosition(head, 3)
    fmt.Println("Deletion done")
}
Aruntime error: invalid memory address or nil pointer dereference
Bcompilation error: missing return statement
Cno error, deletion successful
Druntime error: index out of range
Attempts:
2 left
💡 Hint
Check if the code verifies the current or current.next is nil before accessing.
❓ Predict Output
advanced
2:00remaining
Output after deleting node at invalid position
What is the printed linked list after trying to delete a node at position 5 in a list of length 4?
DSA Go
package main
import "fmt"
type Node struct {
    data int
    next *Node
}
func deleteAtPosition(head *Node, pos int) *Node {
    if head == nil {
        return nil
    }
    if pos == 0 {
        return head.next
    }
    current := head
    for i := 0; current != nil && i < pos-1; i++ {
        current = current.next
    }
    if current == nil || current.next == nil {
        return head
    }
    current.next = current.next.next
    return head
}
func printList(head *Node) {
    for head != nil {
        fmt.Printf("%d -> ", head.data)
        head = head.next
    }
    fmt.Println("null")
}
func main() {
    head := &Node{data: 7}
    head.next = &Node{data: 14}
    head.next.next = &Node{data: 21}
    head.next.next.next = &Node{data: 28}
    head = deleteAtPosition(head, 5)
    printList(head)
}
A7 -> 14 -> 21 -> 28 -> null
B7 -> 14 -> 21 -> null
C14 -> 21 -> 28 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting at a position outside the list length should not change the list.
🧠 Conceptual
expert
2:00remaining
Why is it important to check for nil before deletion?
In the deleteAtPosition function, why must we check if current or current.next is nil before deleting a node?
ATo improve the speed of the deletion operation
BTo avoid runtime errors caused by accessing memory that does not exist
CTo ensure the list is sorted after deletion
DTo automatically update the length of the list
Attempts:
2 left
💡 Hint
Think about what happens if you try to access a property of a nil pointer.

Practice

(1/5)
1. What does deleting a node at a specific position in a linked list mean?
easy
A. Adding a new node at that position
B. Removing the node at that position and linking its previous node to the next node
C. Swapping the node at that position with the last node
D. Counting the number of nodes before that position

Solution

  1. Step 1: Understand node deletion in linked list

    Deleting a node means removing it and connecting the previous node to the next node to keep the list intact.
  2. Step 2: Identify what happens at the specific position

    The node at the given position is removed, and the previous node's next pointer is updated to skip the deleted node.
  3. Final Answer:

    Removing the node at that position and linking its previous node to the next node -> Option B
  4. Quick Check:

    Delete node = remove and link neighbors [OK]
Hint: Delete means remove node and link neighbors [OK]
Common Mistakes:
  • Thinking deletion adds a node
  • Swapping nodes instead of deleting
  • Only counting nodes without deletion
2. Which of the following Go code snippets correctly deletes a node at position pos in a singly linked list starting with head?
easy
A. curr := head; for i := 0; i <= pos; i++ { curr = curr.next }; curr.next = curr.next.next
B. head = head.next; for i := 0; i < pos; i++ { head = head.next }
C. if pos == 0 { head = head.next } else { curr := head; for i := 0; i < pos-1; i++ { curr = curr.next }; curr.next = curr.next.next }
D. for i := 0; i < pos; i++ { head = head.next }; head = head.next.next

Solution

  1. Step 1: Check deletion at position 0

    If position is 0, we update head to head.next to remove the first node.
  2. Step 2: Traverse to node before position and update links

    For other positions, we move to node before the target and set its next to skip the deleted node.
  3. Final Answer:

    if pos == 0 { head = head.next } else { curr := head; for i := 0; i < pos-1; i++ { curr = curr.next }; curr.next = curr.next.next } -> Option C
  4. Quick Check:

    Correct traversal and link update = if pos == 0 { head = head.next } else { curr := head; for i := 0; i < pos-1; i++ { curr = curr.next }; curr.next = curr.next.next } [OK]
Hint: Update previous node's next to skip deleted node [OK]
Common Mistakes:
  • Not handling position 0 separately
  • Looping one extra time causing nil pointer
  • Incorrectly updating head inside loop
3. What is the output linked list after deleting the node at position 2 from the list: 10 -> 20 -> 30 -> 40 -> 50 -> null?
type Node struct {
  data int
  next *Node
}

// Assume head points to 10
// Delete node at position 2
func deleteAtPosition(head *Node, pos int) *Node {
  if pos == 0 {
    return head.next
  }
  curr := head
  for i := 0; i < pos-1; i++ {
    curr = curr.next
  }
  curr.next = curr.next.next
  return head
}
medium
A. 10 -> 20 -> 40 -> 50 -> null
B. 10 -> 20 -> 30 -> 50 -> null
C. 20 -> 30 -> 40 -> 50 -> null
D. 10 -> 30 -> 40 -> 50 -> null

Solution

  1. Step 1: Identify node at position 2

    Position 2 node is the third node with value 30.
  2. Step 2: Update links to remove node 30

    Node at position 1 (20) next pointer is updated to node at position 3 (40), skipping 30.
  3. Final Answer:

    10 -> 20 -> 40 -> 50 -> null -> Option A
  4. Quick Check:

    Deleted 30 at pos 2 = 10->20->40->50 [OK]
Hint: Skip node at position by linking previous to next [OK]
Common Mistakes:
  • Deleting wrong node index
  • Not updating links correctly
  • Returning wrong head pointer
4. The following Go code tries to delete a node at position pos but causes a runtime error. What is the error and how to fix it?
func deleteAtPosition(head *Node, pos int) *Node {
  curr := head
  for i := 0; i < pos; i++ {
    curr = curr.next
  }
  curr.next = curr.next.next
  return head
}
medium
A. Logic error because loop goes to pos instead of pos-1; fix loop to i < pos-1
B. Syntax error due to missing return statement; fix by adding return head
C. No error; code works fine
D. Runtime error due to nil pointer when pos is 0; fix by handling pos 0 separately

Solution

  1. Step 1: Analyze loop traversal

    The loop moves curr to the node at position pos, but we need the node before pos to update links.
  2. Step 2: Fix loop to stop at node before pos

    Change loop condition to i < pos-1 so curr points to previous node, allowing safe link update.
  3. Final Answer:

    Logic error because loop goes to pos instead of pos-1; fix loop to i < pos-1 -> Option A
  4. Quick Check:

    Loop to pos-1 for previous node [OK]
Hint: Loop to node before position, not at position [OK]
Common Mistakes:
  • Looping to pos causing nil pointer
  • Not handling pos 0 case
  • Assuming curr.next always exists
5. Given a singly linked list with nodes: 5 -> 10 -> 15 -> 20 -> 25 -> null, write the resulting list after deleting nodes at positions 0, 2, and then 1 in that order. What is the final list?
hard
A. 10 -> 20 -> null
B. 10 -> 15 -> 25 -> null
C. 5 -> 15 -> 25 -> null
D. 10 -> 25 -> null

Solution

  1. Step 1: Delete node at position 0

    Remove first node (5), list becomes 10 -> 15 -> 20 -> 25 -> null.
  2. Step 2: Delete node at position 2

    Positions now: 0=10,1=15,2=20,3=25. Remove node 20, list becomes 10 -> 15 -> 25 -> null.
  3. Step 3: Delete node at position 1

    Positions now: 0=10,1=15,2=25. Remove node 15, list becomes 10 -> 25 -> null.
  4. Final Answer:

    10 -> 25 -> null -> Option D
  5. Quick Check:

    Delete 0,2,1 in order = 10->25 [OK]
Hint: Update list after each deletion before next [OK]
Common Mistakes:
  • Not updating positions after deletions
  • Deleting wrong nodes due to stale positions
  • Confusing zero-based indexing
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