As reported: Intuit SWE-2 (Bangalore, 2025), phone round (DSA + HLD), first linked-list question: reverse a linked list, both iteratively and recursively.
Given the head of a singly linked list, reverse the list and return the new head. Write both an iterative and a recursive version.
This is LeetCode 206 — Reverse Linked List.
Examples
[1,2,3,4,5] -> [5,4,3,2,1]
[1,2] -> [2,1]
[] -> []
Constraints: 0 ≤ n ≤ 5000; -5000 ≤ Node.val ≤ 5000.
Code runner format: one JSON value per line (LeetCode's test-case format): the list as an array. Implement reverseList(head); the harness prints the returned list as an array.