Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Push Using Linked List Node in DSA Python - 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 the following Python code that pushes nodes to the front of a linked list. What will be the printed linked list after all push operations?

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    def printList(self):
        temp = self.head
        while temp:
            print(temp.data, end=' -> ')
            temp = temp.next
        print('null')

llist = LinkedList()
llist.push(10)
llist.push(20)
llist.push(30)
llist.printList()
A30 -> 10 -> 20 -> null
B10 -> 20 -> 30 -> null
Cnull -> 30 -> 20 -> 10
D30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint

Remember, push adds a new node at the front of the list.

❓ Predict Output
intermediate
2:00remaining
What is the linked list after these push operations?

Given this code snippet, what will be the final linked list printed?

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    def printList(self):
        temp = self.head
        while temp:
            print(temp.data, end=' -> ')
            temp = temp.next
        print('null')

llist = LinkedList()
llist.push(5)
llist.push(15)
llist.push(25)
llist.push(35)
llist.printList()
A5 -> 15 -> 25 -> 35 -> null
B35 -> 25 -> 15 -> 5 -> null
Cnull -> 5 -> 15 -> 25 -> 35
D15 -> 5 -> 25 -> 35 -> null
Attempts:
2 left
💡 Hint

New nodes are added at the front, so the last pushed node is the head.

🔧 Debug
advanced
2:00remaining
Why does this push method cause an error?

Look at this push method in a linked list class. It causes an error when called. What is the cause?

DSA Python
def push(self, new_data):
    new_node = Node(new_data)
    new_node.next = self.head.next
    self.head = new_node
Aself.head might be None, so self.head.next causes AttributeError
BThe method does not assign new_node.data correctly
Cnew_node.next should be None, not self.head.next
Dself.head is not updated properly to new_node
Attempts:
2 left
💡 Hint

Think about what happens if the list is empty before push.

❓ Predict Output
advanced
2:00remaining
What is the linked list after pushing nodes with these values?

Given the following code, what will be the printed linked list?

DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node

    def printList(self):
        temp = self.head
        while temp:
            print(temp.data, end=' -> ')
            temp = temp.next
        print('null')

llist = LinkedList()
for value in [1, 2, 3, 4, 5]:
    llist.push(value)
llist.printList()
A5 -> 4 -> 3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> 4 -> 5 -> null
Cnull -> 1 -> 2 -> 3 -> 4 -> 5
D5 -> 3 -> 1 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint

Push adds each new node at the front, reversing the order.

🧠 Conceptual
expert
2:00remaining
What is the time complexity of pushing a node at the front of a linked list?

Consider a singly linked list. What is the time complexity of the push operation that adds a node at the front?

AO(n^2) - Quadratic time
BO(n) - Linear time
CO(1) - Constant time
DO(log n) - Logarithmic time
Attempts:
2 left
💡 Hint

Think about how many nodes you need to visit to add at the front.

Practice

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

Solution

  1. Step 1: Understand push operation meaning

    Push means adding a new element to the start of the linked list.
  2. Step 2: Identify push effect on linked list

    The new node becomes the head, pointing to the old head node.
  3. Final Answer:

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

    Push = Add at start [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 (removal)
  • Assuming push deletes nodes
2. Which of the following is the correct syntax to create a new node and push it to the front of a linked list in Python?
easy
A. head.next = Node(data); head = head.next
B. head = Node(data); head.next = new_node
C. new_node = Node(data); new_node.next = head; head = new_node
D. new_node = Node(data); head = new_node.next

Solution

  1. Step 1: Create new node with data

    Use new_node = Node(data) to create a node.
  2. Step 2: Link new node to current head and update head

    Set new_node.next = head and then head = new_node.
  3. Final Answer:

    new_node = Node(data); new_node.next = head; head = new_node -> Option C
  4. Quick Check:

    Correct push syntax = new_node = Node(data); new_node.next = head; head = new_node [OK]
Hint: New node points to head, then head updates to new node [OK]
Common Mistakes:
  • Assigning head before linking new_node.next
  • Confusing order of assignments
  • Using undefined variables
3. What will be the linked list after executing the following code?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def push(head, data):
    new_node = Node(data)
    new_node.next = head
    head = new_node
    return head

head = None
head = push(head, 10)
head = push(head, 20)
head = push(head, 30)
medium
A. 30 -> 20 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. 30 -> 10 -> 20 -> null
D. 20 -> 30 -> 10 -> null

Solution

  1. Step 1: Push 10 to empty list

    List becomes 10 -> null.
  2. Step 2: Push 20 to front

    New head is 20, pointing to 10: 20 -> 10 -> null.
  3. Step 3: Push 30 to front

    New head is 30, pointing to 20: 30 -> 20 -> 10 -> null.
  4. Final Answer:

    30 -> 20 -> 10 -> null -> Option A
  5. Quick Check:

    Push adds at front, last pushed is first [OK]
Hint: Last pushed node is always the head [OK]
Common Mistakes:
  • Assuming push adds at end
  • Not updating head after push
  • Mixing order of nodes
4. Find the error in this push function for a linked list:
def push(head, data):
    new_node = Node(data)
    head = new_node
    new_node.next = head
    return head
medium
A. new_node is not created properly
B. new_node.next is set after head is updated, causing a loop
C. head is not returned
D. Function does not accept head as parameter

Solution

  1. Step 1: Analyze assignment order

    head is set to new_node before linking new_node.next.
  2. Step 2: Identify pointer mistake

    new_node.next = head sets next to itself, creating a loop.
  3. Final Answer:

    new_node.next is set after head is updated, causing a loop -> Option B
  4. Quick Check:

    Link new_node.next before updating head [OK]
Hint: Set new_node.next before changing head [OK]
Common Mistakes:
  • Assigning head before linking next
  • Creating self-referential loops
  • Forgetting to return head
5. Given a linked list with nodes 5 -> 15 -> 25 -> null, what will be the list after pushing nodes with values 35 and 45 in order using the push function?
head = Node(5)
head.next = Node(15)
head.next.next = Node(25)
head = push(head, 35)
head = push(head, 45)
hard
A. 45 -> 35 -> 5 -> 15 -> 25 -> null
B. 5 -> 15 -> 25 -> 35 -> 45 -> null
C. 35 -> 45 -> 5 -> 15 -> 25 -> null
D. 45 -> 35 -> 25 -> 15 -> 5 -> null

Solution

  1. Step 1: Original list is 5 -> 15 -> 25 -> null

    Head points to 5, which points to 15, then 25.
  2. Step 2: Push 35

    New node 35 points to head (5), head updates to 35: 35 -> 5 -> 15 -> 25 -> null.
  3. Step 3: Push 45

    New node 45 points to head (35), head updates to 45: 45 -> 35 -> 5 -> 15 -> 25 -> null.
  4. Final Answer:

    45 -> 35 -> 5 -> 15 -> 25 -> null -> Option A
  5. Quick Check:

    Push adds nodes at front in order [OK]
Hint: New nodes become new head, pushing old nodes back [OK]
Common Mistakes:
  • Appending instead of pushing at front
  • Mixing node order after pushes
  • Not updating head after push
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