0
0
DSA Pythonprogramming~20 mins

Find Middle Element of Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middle Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find the middle element output
What is the output of the following code that finds the middle element of a linked list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def find_middle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow.data

# Linked list: 10 -> 20 -> 30 -> 40 -> 50 -> None
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)
head.next.next.next.next = Node(50)

print(find_middle(head))
A50
B20
C40
D30
Attempts:
2 left
💡 Hint
Use two pointers: one moves one step, the other moves two steps.
Predict Output
intermediate
2:00remaining
Middle element in even length list
What is the output of the code when the linked list has an even number of elements?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def find_middle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow.data

# Linked list: 1 -> 2 -> 3 -> 4 -> None
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)

print(find_middle(head))
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
When even number of nodes, the function returns the second middle node.
🔧 Debug
advanced
2:00remaining
Identify the error in middle element code
What error does the following code produce when trying to find the middle element of a linked list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def find_middle(head):
    slow = head
    fast = head
    while fast.next and fast.next.next:
        slow = slow.next
        fast = fast.next.next
    return slow.data

head = None
print(find_middle(head))
AAttributeError
BIndexError
CNone
DNo error, output is 10
Attempts:
2 left
💡 Hint
Check the while loop condition for short lists.
🧠 Conceptual
advanced
1:30remaining
Why use two pointers to find middle?
Why is the two-pointer technique efficient for finding the middle element of a linked list?
AIt traverses the list only once, using two pointers moving at different speeds.
BIt uses extra memory to store all elements and then finds the middle.
CIt sorts the list first and then picks the middle element.
DIt counts the total elements first, then traverses again to the middle.
Attempts:
2 left
💡 Hint
Think about how many times the list is traversed.
Predict Output
expert
2:30remaining
Output of middle element after list modification
What is the output after modifying the linked list and then finding the middle element?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def find_middle(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow.data

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

# Remove the third node (data=3)
head.next.next = head.next.next.next

print(find_middle(head))
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
After removal, the list is 1 -> 2 -> 4 -> 5.