Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

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

Practice

(1/5)
1. What is the main purpose of a Singly Linked List in data structures?
easy
A. To store elements with keys and values for fast lookup
B. To store elements in a sequence where each element points to the next
C. To store elements in a sorted array for binary search
D. To store elements in a tree structure with parent and child nodes

Solution

  1. Step 1: Understand the structure of a singly linked list

    A singly linked list stores elements in nodes where each node points to the next node, forming a chain.
  2. Step 2: Compare with other data structures

    Unlike arrays or trees, singly linked lists do not store keys or have parent-child relationships; they only link sequentially.
  3. Final Answer:

    To store elements in a sequence where each element points to the next -> Option B
  4. Quick Check:

    Singly Linked List = sequence with next pointers [OK]
Hint: Remember: singly linked list nodes point only forward [OK]
Common Mistakes:
  • Confusing linked lists with arrays or hash maps
  • Thinking nodes have backward pointers (that's doubly linked list)
  • Assuming linked lists store key-value pairs
2. Which of the following is the correct way to define a node class for a singly linked list in Python?
easy
A. class Node: def __init__(self, data, next): self.data = data self.next = next self.prev = None
B. class Node: def __init__(self, data): self.data = data self.prev = None
C. class Node: def __init__(self, data): self.data = data self.next = None
D. class Node: def __init__(self): self.data = None self.next = None self.prev = None

Solution

  1. Step 1: Identify the correct attributes for a singly linked list node

    A singly linked list node stores data and a pointer to the next node only, so it needs data and next.
  2. Step 2: Check the constructor parameters and attributes

    class Node: def __init__(self, data): self.data = data self.next = None correctly initializes data and sets next to None by default. Other options include prev which is for doubly linked lists or incorrect parameters.
  3. Final Answer:

    class Node: def __init__(self, data): self.data = data self.next = None -> Option C
  4. Quick Check:

    Node has data and next only [OK]
Hint: Singly linked list nodes have no 'prev' pointer [OK]
Common Mistakes:
  • Adding a 'prev' pointer by mistake
  • Not initializing 'next' to None
  • Using extra parameters unnecessarily
3. What will be the output of the following code that creates a singly linked list with nodes 10, 20, 30 and prints the list?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

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

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=' -> ')
            current = current.next
        print('None')

ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.print_list()
medium
A. 10 -> 20 -> None
B. 30 -> 20 -> 10 -> None
C. None
D. 10 -> 20 -> 30 -> None

Solution

  1. Step 1: Trace the append method calls

    Appending 10 sets head to node with data 10. Then 20 and 30 are added at the end by traversing the list.
  2. Step 2: Trace the print_list method

    Prints nodes in order: 10, then 20, then 30, each followed by ' -> ', ending with 'None'.
  3. Final Answer:

    10 -> 20 -> 30 -> None -> Option D
  4. Quick Check:

    Append adds at end, print shows order [OK]
Hint: Append adds nodes at list end; print shows order [OK]
Common Mistakes:
  • Assuming append adds at front
  • Printing without 'None' at end
  • Confusing order of nodes
4. The following code tries to append nodes to a singly linked list but does not work as expected. What is the error?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

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

ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)

current = ll.head
while current:
    print(current.data, end=' -> ')
    current = current.next
print('None')
medium
A. The line 'last = new_node' does not link the new node to the list
B. The head is never initialized
C. The Node class is missing the 'prev' pointer
D. The while loop condition should be 'while last is not None'

Solution

  1. Step 1: Analyze the append method's else block

    The code finds the last node correctly but then does 'last = new_node' which only changes the local variable, not the last node's next pointer.
  2. Step 2: Identify the fix

    To link the new node, it should be 'last.next = new_node' to update the last node's next pointer.
  3. Final Answer:

    The line 'last = new_node' does not link the new node to the list -> Option A
  4. Quick Check:

    Assign to last.next to link nodes [OK]
Hint: Update last.next, not last variable, to link nodes [OK]
Common Mistakes:
  • Assigning new_node to last instead of last.next
  • Not updating the head when list is empty
  • Confusing variable assignment with pointer update
5. You want to create a singly linked list from scratch that supports inserting a new node at the beginning and printing the list. Which code snippet correctly implements this?
hard
A. class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None')
B. class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.prev = self.head self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None')
C. class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None')
D. class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = None self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None')

Solution

  1. Step 1: Understand insert at beginning logic

    To insert at the beginning, new node's next should point to current head, then head updated to new node.
  2. Step 2: Check each option's insert_at_beginning method

    class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None') correctly sets new_node.next = self.head and updates head. class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.prev = self.head self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None') wrongly uses prev pointer. class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None') misses linking new_node.next. class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def insert_at_beginning(self, data): new_node = Node(data) new_node.next = None self.head = new_node def print_list(self): current = self.head while current: print(current.data, end=' -> ') current = current.next print('None') sets new_node.next to None, losing old list.
  3. Final Answer:

    Option A code snippet correctly inserts at beginning and prints list -> Option A
  4. Quick Check:

    New node points to old head, then head updated [OK]
Hint: New node's next = old head; update head to new node [OK]
Common Mistakes:
  • Not linking new node to old head
  • Using prev pointer in singly linked list
  • Setting new_node.next to None incorrectly
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