#K4906. Reverse a Singly Linked List

    ID: 28558 Type: Default 1000ms 256MiB

Reverse a Singly Linked List

Reverse a Singly Linked List

In this problem, you are given a singly linked list defined by $$ L = {a_1, a_2, \ldots, a_n} $$. Your task is to reverse the list in-place so that the new list becomes $$ L' = {a_n, a_{n-1}, \ldots, a_1} $$. The challenge is to perform this operation by rearranging the pointers of the linked list without using extra space for another list. This problem is a classic example used to test your understanding of linked lists and pointer manipulation.

inputFormat

The input is given via standard input (stdin). The first line contains a single integer ( n ) denoting the number of nodes in the linked list. The second line contains ( n ) space-separated integers representing the values of the nodes in order.

outputFormat

Output the values of the reversed linked list as space-separated integers on a single line via standard output (stdout). If the list is empty, output nothing.## sample

0