#C9472. Reverse a Singly Linked List
Reverse a Singly Linked List
Reverse a Singly Linked List
You are given one or more datasets representing singly linked lists. Each dataset is provided on a separate line containing space‐separated integers. Your task is to reverse each linked list and output the reversed list as space‐separated integers. The input terminates with a line containing a single 0
.
Internally, you should implement the reversal by first converting the input into a linked list, then reversing the linked list using pointer manipulation, and finally converting the reversed linked list back into a list of integers. The reversal process can be described by the formula \(new\_head = reverse(head)\), where the reversal breaks the original pointer chain and reassigns them.
inputFormat
The input consists of several lines. Each line (except the last) contains a test case: a series of space-separated integers representing the nodes of a singly linked list. A line containing only a single 0
signals the end of input. Read input from standard input (stdin).
outputFormat
For each test case (i.e. for each linked list provided except the terminating line), output a single line containing the reversed linked list. The node values must be space-separated. Write output to standard output (stdout).
## sample1 2 3 4 5
10 20 30 40
7 6 5 4
0
5 4 3 2 1
40 30 20 10
4 5 6 7
</p>