Neural Mastery
← Practice
Reverse a Singly Linked Listeasy

Reverse a Singly Linked List

easy Coding Interview Patterns 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Implement iterative linked-list reversal -- the single most-repeated linked-list pattern, and the building block half of the other linked-list problems in this track quietly depend on.

Related Skills

Task

Implement reverse_list(head): walk the list once, at each node redirecting its next pointer to point BACKWARD (to the previous node) instead of forward, using three tracking pointers (previous, current, next).

Function Signature

reverse_list(head: dict | None) -> dict | None

Examples

Example 1: Five-Node List
Input: head = {'val': 1, 'next': {'val': 2, 'next': {'val': 3, 'next': {'val': 4, 'next': {'val': 5, 'next': None}}}}}
Output: {'val': 5, 'next': {'val': 4, 'next': {'val': 3, 'next': {'val': 2, 'next': {'val': 1, 'next': None}}}}}
Example 2: Empty List
Input: head = None
Output: None

Constraints

  • Libraries are allowed; a plain-Python solution earns 10 bonus points.
  • Must run in O(n) time and O(1) extra space (iterative, not building a new list).
  • An empty list (head is None) reverses to itself (still None).
Python3Saved ✓
def reverse_list(head: dict | None) -> dict | None:
"""head: a linked-list node as {'val', 'next'} (None terminates the
list, or represents an empty list). Return the head of the REVERSED
list."""
# Your implementation here
pass

Case 1: Five-Node List
Input: head = {'val': 1, 'next': {'val': 2, 'next': {'val': 3, 'next': {'val': 4, 'next': {'val': 5, 'next': None}}}}}
Expected: {'val': 5, 'next': {'val': 4, 'next': {'val': 3, 'next': {'val': 2, 'next': {'val': 1, 'next': None}}}}}
Case 2: Empty List
Input: head = None
Expected: None