0
0
DSA Pythonprogramming~20 mins

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

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