#K85147. Reverse List of Integers Using a Linked List

    ID: 36577 Type: Default 1000ms 256MiB

Reverse List of Integers Using a Linked List

Reverse List of Integers Using a Linked List

You are given an integer n and a list of n integers. Your task is to reverse the list by simulating linked list operations. Specifically, you need to:

  • Create a singly linked list from the input list.
  • Reverse the linked list.
  • Convert the reversed linked list back to a list of integers.

The process of reversing a linked list can be illustrated by the formula: \[ prev = NULL, \quad current = head \] and iteratively updating pointers so that: \[ current\to next = prev \] until the list is completely reversed.

Write a program that reads input from stdin and outputs the reversed list to stdout in a space-separated format.

inputFormat

The first line of the input contains an integer n, representing the number of integers in the list. If n > 0, the second line contains n space-separated integers. If n = 0, the list is empty.

outputFormat

Output the reversed list of integers as a space-separated sequence in a single line. If the input list is empty, output nothing.

## sample
5
1 2 3 4 5
5 4 3 2 1

</p>