Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Reverse a Singly Linked List Iterative in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A4 -> 3 -> 2 -> null
B1 -> 2 -> 3 -> 4 -> null
Cnull
D4 -> 3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Think about how the pointers are changed in each iteration of the loop.
❓ Predict Output
intermediate
2: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)
A5 -> 5 -> null
B5 -> null
Cnull
DError: NoneType has no attribute 'next'
Attempts:
2 left
💡 Hint
A single node reversed is itself.
🔧 Debug
advanced
2: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)
AAttributeError: 'NoneType' object has no attribute 'next'
BTypeError: unsupported operand type(s) for +: 'Node' and 'Node'
CNo error, returns reversed list
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Check the loop condition and what happens when current is the last node.
❓ Predict Output
advanced
2: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)
Anull
BNone
CError: 'NoneType' object has no attribute 'next'
DEmpty list
Attempts:
2 left
💡 Hint
An empty list reversed is still empty.
🧠 Conceptual
expert
2: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?
A2n times
Bn - 1 times
Cn times
Dn + 1 times
Attempts:
2 left
💡 Hint
Each node's next pointer is changed exactly once during reversal.

Practice

(1/5)
1. What is the main purpose of reversing a singly linked list iteratively?
easy
A. To remove duplicate nodes from the list
B. To sort the list in ascending order
C. To change the direction of all links so the list order is reversed
D. To find the middle node of the list

Solution

  1. Step 1: Understand the linked list structure

    A singly linked list has nodes pointing only to the next node, so reversing means changing these pointers to point backwards.
  2. Step 2: Purpose of iterative reversal

    The iterative method changes each node's next pointer to the previous node, effectively reversing the list order.
  3. Final Answer:

    To change the direction of all links so the list order is reversed -> Option C
  4. Quick Check:

    Reversing means flipping links direction [OK]
Hint: Reversing flips all next pointers to previous nodes [OK]
Common Mistakes:
  • Confusing reversal with sorting
  • Thinking reversal deletes nodes
  • Assuming reversal finds middle node
2. Which of the following is the correct way to initialize pointers for iterative reversal of a singly linked list in Python?
easy
A. prev = head; current = head.next
B. prev = null; current = head
C. prev = head; current = null
D. prev = null; current = null

Solution

  1. Step 1: Understand pointer roles

    Prev starts as null because the new tail will point to null; current starts at head to traverse the list.
  2. Step 2: Correct initialization

    Setting prev = null and current = head allows iteration from the start with no previous node initially.
  3. Final Answer:

    prev = null; current = head -> Option B
  4. Quick Check:

    Prev null, current at head to start [OK]
Hint: Prev starts null, current starts at head [OK]
Common Mistakes:
  • Setting prev to head initially
  • Starting current as null
  • Both pointers as null
3. What will be the output linked list after running the iterative reversal code on this list: 1 -> 2 -> 3 -> null?
medium
A. 3 -> 2 -> 1 -> null
B. 1 -> 2 -> 3 -> null
C. null -> 3 -> 2 -> 1
D. 2 -> 3 -> 1 -> null

Solution

  1. Step 1: Trace reversal process

    Initially: prev=null, current=1. After first iteration, 1 points to null. Then 2 points to 1, then 3 points to 2.
  2. Step 2: Final reversed list

    The list becomes 3 -> 2 -> 1 -> null after all nodes reverse their next pointers.
  3. Final Answer:

    3 -> 2 -> 1 -> null -> Option A
  4. Quick Check:

    Reversed list order is 3, 2, 1 [OK]
Hint: Last node becomes new head after reversal [OK]
Common Mistakes:
  • Not updating pointers correctly
  • Confusing original and reversed order
  • Including null as a node
4. Identify the error in this iterative reversal code snippet:
def reverse(head):
    prev = head
    current = null
    while current:
        next_node = current.next
        current.next = prev
        prev = current
        current = next_node
    return prev
medium
A. prev should start as null, current as head
B. The loop condition should be 'while prev' instead of 'while current'
C. current.next assignment is reversed, should be 'prev.next = current'
D. Function should return current instead of prev

Solution

  1. Step 1: Check pointer initialization

    Prev should be null initially because the new tail points to null; current should start at head to traverse.
  2. Step 2: Loop condition and pointer updates

    Loop runs while current is not null; current.next is set to prev correctly; returning prev gives new head.
  3. Final Answer:

    prev should start as null, current as head -> Option A
  4. Quick Check:

    Prev null, current head to start loop [OK]
Hint: Prev null, current head to avoid skipping nodes [OK]
Common Mistakes:
  • Starting prev at head causes cycle
  • Loop condition incorrect
  • Returning wrong pointer
5. Given a singly linked list with nodes containing values [10, 20, 30, 40, 50], which iterative reversal step correctly updates the pointers after processing the first two nodes?
hard
A. After first node: 10 -> 20; After second node: 20 -> null
B. After first node: 10 -> 20 -> null; After second node: 20 -> 10 -> null
C. After first node: null -> 10; After second node: 10 -> 20
D. After first node: 10 -> null; After second node: 20 -> 10 -> null

Solution

  1. Step 1: Reverse first node pointer

    Initially, prev=null, current=10. After first iteration, 10.next = null, so list is 10 -> null.
  2. Step 2: Reverse second node pointer

    Next, current=20. Set 20.next = prev (which is 10), so list is 20 -> 10 -> null.
  3. Final Answer:

    After first node: 10 -> null; After second node: 20 -> 10 -> null -> Option D
  4. Quick Check:

    Each node points back to previous [OK]
Hint: Each step points current node to previous node [OK]
Common Mistakes:
  • Not setting first node's next to null
  • Linking nodes forward instead of backward
  • Confusing order of pointer updates
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