← 返回 bytedance 的题目列表Reverse Linked List
类型:online_judge
Problem: Reverse Linked List
Given the head node head of a singly linked list, reverse the list and return the new head.
Input Format
For testing, the linked list is represented by an array:
The first line contains an integer n, the number of nodes.
The second line contains n integers, representing node values from head to tail. If n = 0, the second line may be empty or absent.
Output Format
Print the reversed linked list values from head to tail, separated by spaces. If the list is empty, print an empty line.
Constraints
0 <= n <= 5000
-5000 <= Node.val <= 5000
Example
Input:
5
1 2 3 4 5
Output:
5 4 3 2 1
Example
Input
5
1 2 3 4 5
Output
5 4 3 2 1