Bird
Raised Fist0
DSA Goprogramming~20 mins

Push Using Linked List Node in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after pushing nodes in this linked list?
Consider a singly linked list where nodes are pushed to the front. What is the printed linked list after these push operations?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) push(newNode *Node) {
    newNode.next = l.head
    l.head = newNode
}

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

func main() {
    list := LinkedList{}
    list.push(&Node{data: 10})
    list.push(&Node{data: 20})
    list.push(&Node{data: 30})
    list.printList()
}
A30 -> 20 -> 10 -> null
B10 -> 20 -> 30 -> null
C30 -> 10 -> 20 -> null
D20 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember, pushing a node adds it to the front of the list.
❓ Predict Output
intermediate
2:00remaining
What is the output after pushing nodes with duplicate values?
Given the linked list push method, what is the printed list after pushing nodes with values 5, 5, and 10 in that order?
DSA Go
package main

import "fmt"

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) push(newNode *Node) {
    newNode.next = l.head
    l.head = newNode
}

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

func main() {
    list := LinkedList{}
    list.push(&Node{data: 5})
    list.push(&Node{data: 5})
    list.push(&Node{data: 10})
    list.printList()
}
A10 -> 5 -> null
B5 -> 5 -> 10 -> null
C5 -> 10 -> 5 -> null
D10 -> 5 -> 5 -> null
Attempts:
2 left
💡 Hint
Pushing adds nodes to the front regardless of value.
🔧 Debug
advanced
2:00remaining
Why does this push method cause a runtime error?
This push method sometimes causes a runtime panic. What is the cause?
DSA Go
func (l *LinkedList) push(newNode *Node) {
    newNode.next = l.head.next
    l.head = newNode
}
AIt causes an infinite loop in the linked list.
BIt tries to access l.head.next when l.head is nil, causing a nil pointer dereference.
CIt overwrites the entire list with the new node only.
DIt causes a type mismatch error.
Attempts:
2 left
💡 Hint
Check what happens when the list is empty and you access l.head.next.
🧠 Conceptual
advanced
2:00remaining
What is the time complexity of pushing a node to the front of a singly linked list?
Consider the push operation that adds a node at the front of a singly linked list. What is its time complexity?
AO(1) - Constant time
BO(n) - Linear time
CO(log n) - Logarithmic time
DO(n^2) - Quadratic time
Attempts:
2 left
💡 Hint
Pushing only changes the head pointer and the new node's next pointer.
🚀 Application
expert
2:00remaining
After pushing nodes, what is the value of the third node in the list?
Given the push operations below, what is the data value of the third node from the head?
DSA Go
package main

type Node struct {
    data int
    next *Node
}

type LinkedList struct {
    head *Node
}

func (l *LinkedList) push(newNode *Node) {
    newNode.next = l.head
    l.head = newNode
}

func main() {
    list := LinkedList{}
    list.push(&Node{data: 1})
    list.push(&Node{data: 2})
    list.push(&Node{data: 3})
    list.push(&Node{data: 4})
    // What is the data of the third node?
}
A3
B1
C2
D4
Attempts:
2 left
💡 Hint
Remember the order nodes are pushed and how the list looks after each push.

Practice

(1/5)
1. What does the push operation do in a linked list when using a node?
easy
A. Deletes the entire linked list
B. Removes the last node from the list
C. Searches for a node with a specific value
D. Adds a new node at the beginning of the list

Solution

  1. Step 1: Understand push operation in linked list

    The push operation adds a new node at the start (head) of the linked list.
  2. Step 2: Effect of push on list structure

    The new node becomes the head, and its next pointer points to the previous head node.
  3. Final Answer:

    Adds a new node at the beginning of the list -> Option D
  4. Quick Check:

    Push = Add at head [OK]
Hint: Push always adds node at the start of the list [OK]
Common Mistakes:
  • Thinking push adds at the end
  • Confusing push with pop
  • Assuming push deletes nodes
2. Which of the following is the correct syntax to create a new node and push it onto a linked list in Go?
easy
A. newNode := Node{data: 5}; head.next = newNode
B. newNode := &Node{data: 5, next: head}; head = newNode
C. newNode := &Node{5}; head = newNode.next
D. newNode := Node{data: 5, next: nil}; head = newNode.next

Solution

  1. Step 1: Create a new node with data and next pointer

    In Go, use &Node{data: 5, next: head} to create a new node pointing to current head.
  2. Step 2: Update head to new node

    Assign head = newNode to make newNode the new head of the list.
  3. Final Answer:

    newNode := &Node{data: 5, next: head}; head = newNode -> Option B
  4. Quick Check:

    New node points to head, then head updated [OK]
Hint: New node's next points to head, then head updated [OK]
Common Mistakes:
  • Not using pointer (&) for new node
  • Assigning head.next instead of head
  • Setting next to nil instead of head
3. What will be the output linked list after executing this Go code?
type Node struct {
  data int
  next *Node
}

func main() {
  var head *Node = nil
  head = &Node{data: 1, next: head}
  head = &Node{data: 2, next: head}
  head = &Node{data: 3, next: head}
  printList(head)
}

func printList(node *Node) {
  for node != nil {
    fmt.Print(node.data, " -> ")
    node = node.next
  }
  fmt.Print("null")
}
medium
A. 3 -> 2 -> 1 -> null
B. 1 -> 2 -> 3 -> null
C. null
D. 1 -> null

Solution

  1. Step 1: Trace node insertions

    First node with data 1 is pushed, head points to node 1.
    Then node 2 is pushed, head points to node 2, next points to node 1.
    Then node 3 is pushed, head points to node 3, next points to node 2.
  2. Step 2: Print list from head

    Prints 3 -> 2 -> 1 -> null as nodes are added at front.
  3. Final Answer:

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

    Push adds at head, so list reversed [OK]
Hint: Push adds at head, so last pushed is first in list [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Confusing order of nodes
  • Ignoring nil termination
4. Identify the error in this Go code that tries to push a new node onto a linked list:
type Node struct {
  data int
  next *Node
}

func push(head *Node, data int) {
  newNode := &Node{data: data, next: head}
  head = newNode
}

func main() {
  var head *Node = nil
  push(head, 10)
  // printList(head) expected to show 10 -> null
}
medium
A. head is passed by value, so main's head is not updated
B. newNode is not created correctly with & operator
C. next pointer should be nil, not head
D. Function push should return void, not update head

Solution

  1. Step 1: Analyze parameter passing

    head is passed by value to push, so changes inside push do not affect main's head.
  2. Step 2: Effect on linked list head

    Assigning head = newNode inside push only changes local copy, main's head remains nil.
  3. Final Answer:

    head is passed by value, so main's head is not updated -> Option A
  4. Quick Check:

    Pointer must be passed by reference or returned [OK]
Hint: Pass head pointer by reference or return new head [OK]
Common Mistakes:
  • Assuming pointer changes reflect outside function
  • Not returning updated head
  • Misunderstanding Go parameter passing
5. Given a linked list with nodes containing integers, how would you modify the push function to avoid memory leaks and ensure the list is correctly updated in Go?
hard
A. Use a slice instead of linked list for better memory management
B. Use a global variable for head instead of passing it
C. Make push return the new head node and assign it to head in main
D. Set newNode.next to nil before assigning head

Solution

  1. Step 1: Understand pointer update requirement

    In Go, to update head outside push, function must return new head node.
  2. Step 2: Assign returned head in main

    Call head = push(head, data) to update head correctly and avoid dangling pointers.
  3. Final Answer:

    Make push return the new head node and assign it to head in main -> Option C
  4. Quick Check:

    Return new head to update list safely [OK]
Hint: Return new head from push and assign it outside [OK]
Common Mistakes:
  • Not returning updated head
  • Using global variables unnecessarily
  • Ignoring pointer semantics in Go
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