Challenge - 5 Problems
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the linked list after reversal?
Given the following code that reverses a singly linked list iteratively, what will be the printed linked list after the reversal?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None def reverse_linked_list(head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev def print_list(head): current = head result = [] while current: result.append(str(current.val)) current = current.next print(' -> '.join(result) + ' -> null') # Create linked list: 1 -> 2 -> 3 -> 4 -> null head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) # Reverse the list reversed_head = reverse_linked_list(head) # Print reversed list print_list(reversed_head)
Attempts:
2 left
💡 Hint
Think about how the pointers are changed in each iteration of the loop.
✗ Incorrect
The iterative reversal changes the next pointer of each node to point to the previous node, effectively reversing the list order. The final list starts with the original last node.
❓ Predict Output
intermediate2:00remaining
What is the output after reversing a single-node linked list?
What will be the output of the following code after reversing a linked list with only one node?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None def reverse_linked_list(head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev def print_list(head): current = head result = [] while current: result.append(str(current.val)) current = current.next print(' -> '.join(result) + ' -> null') # Create linked list: 5 -> null head = Node(5) # Reverse the list reversed_head = reverse_linked_list(head) # Print reversed list print_list(reversed_head)
Attempts:
2 left
💡 Hint
A single node reversed is itself.
✗ Incorrect
Reversing a single-node list results in the same list because there is no other node to change the order.
🔧 Debug
advanced2:00remaining
What error does this code raise when reversing a linked list?
Identify the error raised by the following code when trying to reverse a linked list.
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None def reverse_linked_list(head): prev = None current = head while current.next: next_node = current.next current.next = prev prev = current current = next_node return prev # Create linked list: 1 -> 2 -> null head = Node(1) head.next = Node(2) # Reverse the list reversed_head = reverse_linked_list(head)
Attempts:
2 left
💡 Hint
Check the loop condition and what happens when current is the last node.
✗ Incorrect
The loop condition 'while current.next' fails when current is the last node (current.next is None), so the loop stops early and the last node is not processed. But the function returns prev which is not the new head. This causes the reversed list to be incomplete and may cause errors if used further.
❓ Predict Output
advanced2:00remaining
What is the output after reversing an empty linked list?
What will be printed after reversing an empty linked list (head is None)?
DSA Python
def reverse_linked_list(head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev def print_list(head): current = head result = [] while current: result.append(str(current.val)) current = current.next print(' -> '.join(result) + ' -> null') head = None reversed_head = reverse_linked_list(head) print_list(reversed_head)
Attempts:
2 left
💡 Hint
An empty list reversed is still empty.
✗ Incorrect
Since the list is empty, the reversed list is also empty. The print function prints 'null' to indicate the end of the list.
🧠 Conceptual
expert2:00remaining
How many pointer reassignments occur when reversing a linked list of length n iteratively?
Consider a singly linked list with n nodes. When reversing it iteratively using the standard approach, how many times is the 'next' pointer reassigned in total?
Attempts:
2 left
💡 Hint
Each node's next pointer is changed exactly once during reversal.
✗ Incorrect
In the iterative reversal, each node's next pointer is reassigned exactly once to point to the previous node. Since there are n nodes, there are n pointer reassignments.