Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Node Structure and Pointer Design in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Node Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a singly linked list after insertions
What is the printed output 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 LinkedList:
    def __init__(self):
        self.head = None

    def insert_end(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('null')

ll = LinkedList()
ll.insert_end(10)
ll.insert_end(20)
ll.insert_end(30)
ll.print_list()
A10 -> 20 -> 30 -> null
B30 -> 20 -> 10 -> null
Cnull
D10 -> 30 -> 20 -> null
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end of the list.
❓ Predict Output
intermediate
2:00remaining
Output after reversing a singly linked list
What is the output of the linked list after reversing it?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_end(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 reverse(self):
        prev = None
        current = self.head
        while current:
            next_node = current.next
            current.next = prev
            prev = current
            current = next_node
        self.head = prev

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

ll = LinkedList()
ll.insert_end(1)
ll.insert_end(2)
ll.insert_end(3)
ll.reverse()
ll.print_list()
A1 -> 2 -> 3 -> null
B3 -> 2 -> 1 -> null
Cnull
D1 -> 3 -> 2 -> null
Attempts:
2 left
💡 Hint
Reversing changes the direction of the pointers.
🔧 Debug
advanced
2:00remaining
Identify the error in doubly linked list node insertion
What error will occur when running this code that inserts a node at the beginning of a doubly linked list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

    def insert_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        if self.head:
            self.head.prev = new_node
        self.head = new_node

dll = DoublyLinkedList()
dll.insert_beginning(5)
dll.insert_beginning(10)
print(dll.head.prev.data)
A5
B10
CAttributeError: 'NoneType' object has no attribute 'data'
DNone
Attempts:
2 left
💡 Hint
Check what happens to the prev pointer of the head node.
🧠 Conceptual
advanced
1:30remaining
Pointer design in circular linked list
In a circular singly linked list with one node, what does the node's next pointer point to?
AIt points to itself
BIt points to None
CIt points to a new node
DIt points to the head's previous node
Attempts:
2 left
💡 Hint
In circular lists, the last node connects back to the first node.
❓ Predict Output
expert
2:30remaining
Output after complex pointer manipulation in linked list
What is the output after executing the following code that manipulates pointers in a 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 insert_end(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 manipulate(self):
        if not self.head or not self.head.next:
            return
        first = self.head
        second = first.next
        third = second.next
        # Swap first and second nodes by changing pointers
        second.next = first
        first.next = third
        self.head = second

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

ll = LinkedList()
ll.insert_end(1)
ll.insert_end(2)
ll.insert_end(3)
ll.manipulate()
ll.print_list()
A1 -> 2 -> 3 -> null
B1 -> 3 -> 2 -> null
C3 -> 2 -> 1 -> null
D2 -> 1 -> 3 -> null
Attempts:
2 left
💡 Hint
The manipulate method swaps the first two nodes by changing pointers.

Practice

(1/5)
1. What does the next pointer in a linked list node represent?
easy
A. It points to the previous node in the list
B. It stores the data value of the node
C. It points to the next node in the list
D. It stores the size of the list

Solution

  1. Step 1: Understand the role of next pointer

    The next pointer in a linked list node is used to connect the current node to the following node in the sequence.
  2. Step 2: Differentiate from other node parts

    The data part stores the value, and next specifically points to the next node, not previous or size.
  3. Final Answer:

    It points to the next node in the list -> Option C
  4. Quick Check:

    next pointer = next node address [OK]
Hint: Remember: next always leads forward in the list [OK]
Common Mistakes:
  • Confusing next with data storage
  • Thinking next points backward
  • Assuming next stores list size
2. Which of the following is the correct way to define a simple node class in Python with a data attribute and a next pointer?
easy
A. class Node: def __init__(self, data): self.data = data self.next = None
B. class Node: def __init__(self, data): data = self.data next = None
C. class Node: def __init__(self): self.data = None self.next = None
D. class Node: def __init__(self, data, next): self.data = data next = None

Solution

  1. Step 1: Check correct attribute assignment

    class Node: def __init__(self, data): self.data = data self.next = None correctly assigns self.data = data and initializes self.next = None to indicate no next node yet.
  2. Step 2: Identify errors in other options

    class Node: def __init__(self, data): data = self.data next = None assigns backwards, class Node: def __init__(self): self.data = None self.next = None misses data parameter, class Node: def __init__(self, data, next): self.data = data next = None does not assign self.next properly.
  3. Final Answer:

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

    Correct attribute setup = class Node: def __init__(self, data): self.data = data self.next = None [OK]
Hint: Use self.attribute = value inside __init__ [OK]
Common Mistakes:
  • Not using self to assign attributes
  • Forgetting to initialize next to None
  • Swapping assignment order
3. What will be the output of the following code?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(10)
second = Node(20)
head.next = second
print(head.next.data)
medium
A. 20
B. Error
C. None
D. 10

Solution

  1. Step 1: Understand node creation and linking

    Two nodes are created: head with data 10, second with data 20. Then, head.next points to second.
  2. Step 2: Trace the print statement

    head.next.data accesses the data of the node after head, which is second, so it prints 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    head.next.data = 20 [OK]
Hint: Follow the next pointer to get next node's data [OK]
Common Mistakes:
  • Printing head.data instead of head.next.data
  • Confusing next pointer with data
  • Expecting None because of uninitialized pointer
4. Identify the error in the following code snippet for a singly linked list node:
class Node:
    def __init__(self, data):
        self.data = data
        next = None

node1 = Node(5)
print(node1.next)
medium
A. The class name should be lowercase
B. The data attribute is missing
C. The print statement syntax is wrong
D. The next pointer is not assigned to self

Solution

  1. Step 1: Check attribute assignment in __init__

    The code assigns next = None without self., so next is a local variable, not an attribute.
  2. Step 2: Understand consequence on print

    Since node1.next was never set, it raises an AttributeError or prints None if accessed differently.
  3. Final Answer:

    The next pointer is not assigned to self -> Option D
  4. Quick Check:

    Missing self. causes attribute error [OK]
Hint: Always use self. to create object attributes [OK]
Common Mistakes:
  • Forgetting self. before next
  • Assuming local variable becomes attribute
  • Ignoring error from missing attribute
5. You want to create a linked list with three nodes containing values 1, 2, and 3 respectively. Which code snippet correctly links the nodes?
hard
A. n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = n3 n3.next = n2 n2.next = None
B. n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = n2 n2.next = n3 n3.next = None
C. n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = None n2.next = n1 n3.next = n2
D. n1 = Node(1) n2 = Node(2) n3 = Node(3) n3.next = n2 n2.next = n1 n1.next = None

Solution

  1. Step 1: Understand correct node linking order

    To create a linked list 1 -> 2 -> 3, n1.next should point to n2, and n2.next to n3. The last node n3.next should be None.
  2. Step 2: Check each option's linking

    n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = n2 n2.next = n3 n3.next = None correctly links nodes in order. The other options link nodes in wrong order or reverse.
  3. Final Answer:

    n1.next = n2; n2.next = n3; n3.next = None -> Option B
  4. Quick Check:

    Correct forward linking = n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = n2 n2.next = n3 n3.next = None [OK]
Hint: Link nodes in order, last node points to None [OK]
Common Mistakes:
  • Linking nodes in reverse order
  • Not setting last node's next to None
  • Mixing node references
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