0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 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.