#C14159. Linked List Operations
Linked List Operations
Linked List Operations
You are given a linked list and an operation command. The linked list is represented by a sequence of integers. The command can be either reverse
or palindrome
.
If the command is reverse
, you need to reverse the linked list and output it in the format:
node1 -> node2 -> ... -> None
If the command is palindrome
, you need to check whether the linked list is a palindrome. A linked list is considered a palindrome if reading the list from head to tail yields the same sequence as reading it from tail to head. Note that an empty list is considered a palindrome.
Input/Output must be handled using standard input (stdin) and standard output (stdout).
Example:
Input: reverse 5 1 2 3 4 5</p>Output: 5 -> 4 -> 3 -> 2 -> 1 -> None
Input: palindrome 5 1 2 3 2 1
Output: True
inputFormat
The input consists of:
- A string that denotes the operation, which can be either
reverse
orpalindrome
. - An integer n representing the number of nodes in the linked list.
- If n > 0, a line containing n space-separated integers representing the node values. If n = 0, the list is empty.
All input is given via standard input (stdin).
outputFormat
If the operation is reverse
, output the linked list in reversed order in the format:
node1 -> node2 -> ... -> None
If the operation is palindrome
, output True
if the linked list is a palindrome, otherwise output False
.
All output should be printed to standard output (stdout).
## samplereverse
5
1 2 3 4 5
5 -> 4 -> 3 -> 2 -> 1 -> None
</p>