0
0
DSA Pythonprogramming~20 mins

Reorder Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reorder Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Reordering a Linked List
What is the printed state of the linked list after running the reorder function on the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Python
class Node:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def print_list(head):
    result = []
    while head:
        result.append(str(head.val))
        head = head.next
    print(' -> '.join(result) + ' -> null')

def reorder_list(head):
    if not head or not head.next:
        return
    # Find middle
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    # Reverse second half
    prev, curr = None, slow.next
    slow.next = None
    while curr:
        nxt = curr.next
        curr.next = prev
        prev = curr
        curr = nxt
    # Merge two halves
    first, second = head, prev
    while second:
        tmp1, tmp2 = first.next, second.next
        first.next = second
        second.next = tmp1
        first, second = tmp1, tmp2

head = Node(1, Node(2, Node(3, Node(4, Node(5)))))
reorder_list(head)
print_list(head)
A1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 5 -> 2 -> 4 -> 3 -> null
C5 -> 4 -> 3 -> 2 -> 1 -> null
D1 -> 3 -> 5 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint
Think about splitting the list, reversing the second half, then merging.
🧠 Conceptual
intermediate
1:00remaining
Understanding the Purpose of Reordering
What is the main goal of the 'Reorder Linked List' algorithm?
ATo rearrange the list so that nodes alternate from the start and end towards the center.
BTo sort the linked list in ascending order.
CTo remove duplicate nodes from the linked list.
DTo reverse the entire linked list.
Attempts:
2 left
💡 Hint
Think about how the first and last nodes are placed after reordering.
🔧 Debug
advanced
2:00remaining
Identify the Error in Reordering Code
What error will this code produce when trying to reorder the list 1 -> 2 -> 3 -> 4 -> null? class Node: def __init__(self, val=0, next=None): self.val = val self.next = next def reorder_list(head): slow, fast = head, head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next prev, curr = None, slow.next slow.next = None while curr: nxt = curr.next curr.next = prev prev = curr curr = nxt first, second = head, prev while second: tmp1 = first.next tmp2 = second.next first.next = second second.next = tmp1 first = tmp1 second = tmp2 head = Node(1, Node(2, Node(3, Node(4)))) reorder_list(head)
AAttributeError because 'NoneType' object has no attribute 'next'
BNo error, code runs correctly
CTypeError due to wrong variable assignment
DInfinite loop causing program to hang
Attempts:
2 left
💡 Hint
Check the while loop condition for finding the middle.
📝 Syntax
advanced
1:30remaining
Correct the Syntax Error in Reorder Function
Which option fixes the syntax error in this code snippet? reorder_list = lambda head: ( slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next )
AAdd parentheses around the while loop inside lambda
BRemove the while loop and use a for loop instead
CReplace lambda with def and add colon after function name
DAdd a return statement inside the lambda
Attempts:
2 left
💡 Hint
Lambda functions cannot contain statements like loops.
🚀 Application
expert
1:00remaining
Number of Nodes After Reordering
Given a linked list with 7 nodes, after applying the reorder list algorithm, how many nodes will be in the final list?
A5 nodes
B6 nodes
C8 nodes
D7 nodes
Attempts:
2 left
💡 Hint
Reordering changes order but not the number of nodes.