#K64712. Reverse a Singly Linked List

    ID: 32037 Type: Default 1000ms 256MiB

Reverse a Singly Linked List

Reverse a Singly Linked List

Given a singly linked list, write a function to reverse it in-place.

You will be provided with a list of integers representing the nodes of the linked list. Your task is to reverse the linked list and output the values of the new list.

Note that the reversal must be performed in-place with O(1) extra space. Use the iterative approach for reversing the linked list.

inputFormat

The input is provided via stdin. The first line contains an integer ( n ), denoting the number of nodes in the linked list. The second line contains ( n ) space-separated integers representing the node values. When ( n = 0 ), the list is empty.

outputFormat

Output the node values of the reversed linked list in one line via stdout. The values should be separated by a single space. If the list is empty, output an empty line.## sample

5
1 2 3 4 5
5 4 3 2 1