#K96057. Reverse a Singly Linked List

    ID: 39001 Type: Default 1000ms 256MiB

Reverse a Singly Linked List

Reverse a Singly Linked List

You are given a singly linked list. Your task is to reverse the linked list and print its nodes using the given format. The list is constructed from nodes where each node contains an integer value and a pointer to the next node.

The reversal must be performed iteratively with a time complexity of \(O(n)\) and a constant extra space complexity \(O(1)\). The output should list the node values separated by an arrow (->) and end with None. For instance, a reversed list with nodes 1, 2, 3 should be printed as:

3 -> 2 -> 1 -> None

inputFormat

The first line contains a non-negative integer \(n\) representing the number of nodes. If \(n > 0\), the second line contains \(n\) space-separated integers indicating the values of the nodes in the original order.

outputFormat

Output a single line containing the reversed linked list in the format:

a -> b -> ... -> None

For an empty list (\(n = 0\)), simply output None.

## sample
1
1
1 -> None