Bird
Raised Fist0
DSA Cprogramming~10 mins

Reverse a Singly Linked List Iterative in DSA C - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the previous pointer to NULL before starting the reversal.

DSA C
struct Node* reverseList(struct Node* head) {
    struct Node* prev = [1];
    struct Node* current = head;
    struct Node* next = NULL;
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    return prev;
}
Drag options to blanks, or click blank then click option'
ANULL
Bhead
Ccurrent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing prev to head instead of NULL causes incorrect links.
Using uninitialized pointers leads to runtime errors.
2fill in blank
medium

Complete the code to update the next pointer to the next node in the list inside the loop.

DSA C
while (current != NULL) {
    [1] = current->next;
    current->next = prev;
    prev = current;
    current = next;
}
Drag options to blanks, or click blank then click option'
Aprev
Bhead
Ccurrent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning prev or current instead of next causes loss of list nodes.
Not saving next before changing links breaks the list.
3fill in blank
hard

Fix the error in the return statement to return the new head of the reversed list.

DSA C
return [1];
Drag options to blanks, or click blank then click option'
Acurrent
Bprev
Chead
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Returning head returns the original list, not reversed.
Returning current or next leads to NULL or incomplete list.
4fill in blank
hard

Fill both blanks to complete the while loop condition and update current pointer correctly.

DSA C
while ([1] != NULL) {
    next = current->next;
    current->next = prev;
    prev = current;
    current = [2];
}
Drag options to blanks, or click blank then click option'
Acurrent
Bprev
Cnext
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev in the loop condition causes infinite loop.
Updating current to prev or head breaks traversal.
5fill in blank
hard

Fill all three blanks to complete the iterative reversal function correctly.

DSA C
struct Node* reverseList(struct Node* head) {
    struct Node* prev = [1];
    struct Node* current = head;
    struct Node* next = NULL;
    while (current != [2]) {
        next = current->next;
        current->next = [3];
        prev = current;
        current = next;
    }
    return prev;
}
Drag options to blanks, or click blank then click option'
ANULL
Bhead
Cprev
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of NULL for prev initialization.
Wrong loop condition causes infinite or no loop.
Setting current->next to wrong pointer breaks the list.

Practice

(1/5)
1. What is the main purpose of the iterative method to reverse a singly linked list?
easy
A. To delete all nodes from the linked list
B. To create a new linked list with reversed elements
C. To sort the linked list in ascending order
D. To change the direction of the links between nodes one by one

Solution

  1. Step 1: Understand the linked list structure

    A singly linked list has nodes connected in one direction only, from head to tail.
  2. Step 2: Purpose of iterative reversal

    The iterative method changes the direction of each node's pointer to point to the previous node, reversing the list.
  3. Final Answer:

    To change the direction of the links between nodes one by one -> Option D
  4. Quick Check:

    Reversing means changing links direction [OK]
Hint: Reversing means flipping links, not creating new nodes [OK]
Common Mistakes:
  • Thinking reversal creates a new list instead of changing links
  • Confusing reversal with sorting
  • Assuming nodes are deleted during reversal
2. Which of the following is the correct way to initialize pointers for iterative reversal of a singly linked list in C?
easy
A. struct Node *prev = NULL, *current = head, *next = NULL;
B. struct Node *prev = head, *current = NULL, *next = NULL;
C. struct Node *prev = NULL, *current = NULL, *next = head;
D. struct Node *prev = head, *current = head, *next = head;

Solution

  1. Step 1: Identify starting pointers

    For reversal, prev starts as NULL because the new tail points to NULL, current starts at head to traverse.
  2. Step 2: Correct initialization

    Next pointer is initialized as NULL and updated inside the loop; so initial next = NULL is correct.
  3. Final Answer:

    struct Node *prev = NULL, *current = head, *next = NULL; -> Option A
  4. Quick Check:

    Prev NULL, current head to start [OK]
Hint: Prev starts NULL, current starts at head [OK]
Common Mistakes:
  • Setting prev to head initially
  • Setting current to NULL at start
  • Initializing next pointer incorrectly
3. What will be the output linked list after running the iterative reverse function on the list: 1 -> 2 -> 3 -> NULL?
medium
A. 2 -> 3 -> 1 -> NULL
B. 3 -> 2 -> 1 -> NULL
C. NULL -> 3 -> 2 -> 1
D. 1 -> 2 -> 3 -> NULL

Solution

  1. Step 1: Trace reversal process

    Initially: 1->2->3->NULL. After reversal, links point backward: 3->2->1->NULL.
  2. Step 2: Confirm final head

    The last node (3) becomes the new head, so output is 3->2->1->NULL.
  3. Final Answer:

    3 -> 2 -> 1 -> NULL -> Option B
  4. Quick Check:

    Reverse order of nodes [OK]
Hint: Reverse means last becomes first [OK]
Common Mistakes:
  • Assuming list stays the same after reversal
  • Confusing NULL placement
  • Mixing node order incorrectly
4. Identify the error in this iterative reversal code snippet for a singly linked list in C:
struct Node* reverse(struct Node* head) {
  struct Node* prev = head;
  struct Node* current = head;
  struct Node* next = NULL;
  while (current != NULL) {
    next = current->next;
    current->next = prev;
    prev = current;
    current = next;
  }
  return prev;
}
medium
A. prev should be initialized to NULL, not head
B. current should be initialized to NULL, not head
C. next pointer is not updated inside the loop
D. Function should return current, not prev

Solution

  1. Step 1: Check pointer initialization

    Prev must start as NULL because the new tail node points to NULL, not head.
  2. Step 2: Analyze loop pointer updates

    Current and next are correctly updated; only prev initialization is wrong.
  3. Final Answer:

    prev should be initialized to NULL, not head -> Option A
  4. Quick Check:

    Prev NULL to mark new tail [OK]
Hint: Prev must start NULL to avoid cycle [OK]
Common Mistakes:
  • Starting prev at head causing cycle
  • Not updating next inside loop
  • Returning wrong pointer at end
5. Given a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> NULL, write the final linked list after applying iterative reversal twice in a row.
hard
A. NULL
B. 40 -> 30 -> 20 -> 10 -> NULL
C. 10 -> 20 -> 30 -> 40 -> NULL
D. 20 -> 10 -> 40 -> 30 -> NULL

Solution

  1. Step 1: Reverse the list once

    Original: 10->20->30->40->NULL. After first reversal: 40->30->20->10->NULL.
  2. Step 2: Reverse the reversed list again

    Reversing 40->30->20->10->NULL returns it to original: 10->20->30->40->NULL.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> NULL -> Option C
  4. Quick Check:

    Double reversal restores original list [OK]
Hint: Reversing twice returns original list [OK]
Common Mistakes:
  • Assuming double reversal changes list order
  • Confusing node values after reversal
  • Forgetting NULL at list end
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