Quick 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.

Reverse a Singly Linked List by Relinking Its Nodes

Company: Autodesk

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given the head of a singly linked list, reverse the list and return the head of the reversed list. Each node is a `ListNode` object with an integer field `val` and a field `next` that refers to the following node, or is `None` for the last node. ### Function Signature ```python def reverse_list(head: Optional[ListNode]) -> Optional[ListNode]: ``` ### Rules - `ListNode` has exactly two fields: `val` (an integer) and `next` (a `ListNode` or `None`). - An empty list is represented by `None`; reversing it returns `None`. - Reverse the list by changing the `next` fields of the existing nodes. Do not create new nodes and do not change any `val`. - The examples write a list as the values of its nodes from head to tail. ### Constraints - The list has between `0` and `5000` nodes. - `-10^9 <= val <= 10^9` for every node. ### Examples #### Example 1 ```text Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] ``` The old tail becomes the head, and the old head becomes the tail with `next = None`. #### Example 2 ```text Input: head = [7,7,2] Output: [2,7,7] ``` Nodes with equal values move like any other node. #### Example 3 ```text Input: head = [] Output: [] ``` The empty list stays empty.

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

Given the head of a singly linked list, reverse the list and return the head of the reversed list. Each node is a `ListNode` with exactly two fields: an integer `val`, and `next`, which refers to the following node or is `None` for the last node. Reverse the list by changing the `next` fields of the existing nodes. Do not create new nodes and do not change any `val`. An empty list is represented by `None`, and reversing it returns `None`. ### Console format A list is written as the values of its nodes from head to tail, the same convention the examples use. Your function receives `head` in that form (an empty list is `[]`) and returns the reversed list in the same form. Grading compares the returned values only, so the rule about re-pointing `next` fields states the intended technique and cannot be checked by the grader. #### Example 1 ```text Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] ``` The old tail becomes the head, and the old head becomes the tail with `next = None`. #### Example 2 ```text Input: head = [7,7,2] Output: [2,7,7] ``` Nodes with equal values move like any other node. ### Constraints - The list has between 0 and 5000 nodes. - `-10^9 <= val <= 10^9` for every node. - No value exceeds 2^31-1 in magnitude, so Java and C++ use `int`.

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

  1. 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.
  2. Work out what the old head's `next` must hold after the reversal, and what to return when the list is empty.
  3. Equal values need no special treatment, since the task moves links between nodes and never compares values.

Reverse a Singly Linked List

easyAutodeskSoftware Engineer

Given the head of a singly linked list, reverse the list and return the head of the reversed list. Each node is a ListNode with exactly two fields: an integer val, and next, which refers to the following node or is None for the last node.

Reverse the list by changing the next fields of the existing nodes. Do not create new nodes and do not change any val. An empty list is represented by None, and reversing it returns None.

Console format

A list is written as the values of its nodes from head to tail, the same convention the examples use. Your function receives head in that form (an empty list is []) and returns the reversed list in the same form. Grading compares the returned values only, so the rule about re-pointing next fields states the intended technique and cannot be checked by the grader.

Example 1

Input:  head = [1,2,3,4,5]
Output: [5,4,3,2,1]

The old tail becomes the head, and the old head becomes the tail with next = None.

Example 2

Input:  head = [7,7,2]
Output: [2,7,7]

Nodes with equal values move like any other node.

Constraints

  • The list has between 0 and 5000 nodes.
  • -10^9 <= val <= 10^9 for every node.
  • No value exceeds 2^31-1 in magnitude, so Java and C++ use int.

Examples

Example 1
Input
([],)
Output
[]
Notes
An empty list (None) reverses to an empty list.
Example 2
Input
([42],)
Output
[42]
Notes
A single node is its own reversal; its next stays None.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def reverse_list(head: list[int]) -> list[int]:
Sample call
reverse_list([])
Language
Case 1
Input
([],)
Expected
[]
Case 2
Input
([42],)
Expected
[42]

Browse More Questions

Show the approach

Approach

Walk the list once while holding two references: prev, the head of the part already reversed (initially None), and curr, the next node still to visit (initially head). At each step save curr.next as following, set curr.next = prev, then move prev = curr and curr = following. Invariant: before every step, prev heads the visited nodes in reverse order, with the old head at the end pointing to None, and curr heads the untouched rest of the original list. Each step moves exactly one node from the rest to the front of the reversed part, so when curr becomes None every node has been re-pointed and prev is the new head. Only next fields change, so no node is created and no val is altered. The reference solutions model node i as position i of the value array and its next field as entry i of an index array (-1 stands for None), run exactly this loop on the index array, then read the values from the new head to build the returned list. Edge cases: an empty list skips the loop and returns []; a single node keeps next = None and is returned as is; equal values need no special handling because only links move. The pointer reversal itself uses O(1) extra space; the index array and the returned list in the console's array form take O(n).

Time complexity:
O(n)
Space complexity:
O(n)