Reverse a Singly Linked List by Relinking Its Nodes
Company: Autodesk
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: Reverse a singly linked list by relinking its existing nodes and return the head of the reversed list. It tests careful pointer manipulation and correct handling of empty and single-node lists.
Read the full Autodesk Software Engineer interview experience this question came from
Constraints
- The list has between 0 and 5000 nodes (an empty list is written [] in this console).
- -10^9 <= val <= 10^9 for every node.
- No value exceeds 2^31-1 in magnitude, so Java and C++ use int.
Examples
Input: ([],)
Expected Output: []
Explanation: An empty list (None) reverses to an empty list.
Input: ([42],)
Expected Output: [42]
Explanation: A single node is its own reversal; its next stays None.
Hints
- Changing a node's `next` field cuts that node off from the rest of the original list. Decide what you need to remember before you change it.
- Work out what the old head's `next` must hold after the reversal, and what to return when the list is empty.
- Equal values need no special treatment, since the task moves links between nodes and never compares values.