0
0
DSA Pythonprogramming~20 mins

Creating a Singly Linked List from Scratch in DSA Python - Debugging & Practice

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Inserting Nodes in a Singly Linked List
What is the printed state of the linked list after inserting nodes with values 10, 20, and 30 in that order?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_end(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=' -> ' if current.next else ' -> null\n')
            current = current.next

sll = SinglyLinkedList()
sll.insert_at_end(10)
sll.insert_at_end(20)
sll.insert_at_end(30)
sll.print_list()
A30 -> 20 -> 10 -> null
B10 -> 20 -> 30 -> null
C10 -> 30 -> 20 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
🧠 Conceptual
intermediate
1:00remaining
Understanding the Head Pointer in a Singly Linked List
What does the 'head' pointer in a singly linked list represent?
AIt points to the last node in the list.
BIt points to the middle node in the list.
CIt points to the first node in the list.
DIt points to a random node in the list.
Attempts:
2 left
💡 Hint
Think about where the list starts when you want to traverse it.
🔧 Debug
advanced
2:00remaining
Identify the Error in Node Insertion Code
What error will occur when running this code to insert a node at the end of the list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_end(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node

sll = SinglyLinkedList()
sll.insert_at_end(5)
sll.insert_at_end(15)
sll.insert_at_end(25)
print(sll.head.next.next.next.data)
AAttributeError: 'NoneType' object has no attribute 'data'
BNo error, prints 25
CIndexError: list index out of range
DTypeError: 'int' object is not callable
Attempts:
2 left
💡 Hint
Check how many nodes are in the list and what happens when accessing next pointers.
Predict Output
advanced
2:00remaining
Output After Deleting a Node from Singly Linked List
What is the printed state of the linked list after deleting the node with value 20?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_end(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

    def delete_node(self, key):
        current = self.head
        prev = None
        while current and current.data != key:
            prev = current
            current = current.next
        if not current:
            return
        if prev is None:
            self.head = current.next
        else:
            prev.next = current.next

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=' -> ' if current.next else ' -> null\n')
            current = current.next

sll = SinglyLinkedList()
sll.insert_at_end(10)
sll.insert_at_end(20)
sll.insert_at_end(30)
sll.delete_node(20)
sll.print_list()
A30 -> 10 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D10 -> 30 -> null
Attempts:
2 left
💡 Hint
Deleting a node removes it from the chain, so it won't appear in the printed list.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Searching in a Singly Linked List
What is the time complexity of searching for a value in a singly linked list with n nodes?
AO(n)
BO(1)
CO(n log n)
DO(log n)
Attempts:
2 left
💡 Hint
You may need to check each node one by one in the worst case.