Skip to content
QuantReadySign In
#092easyLinked List

Reverse Order Book

Time Limit: 2sMemory: 256MB

Problem

An order book is represented as a singly linked list where each node contains a price level. The trading system needs to reverse the order book so that the last price level becomes the first and vice versa.

Given the head of a singly linked list, reverse it in-place and return the new head.

Input Format

A single line of space-separated integers representing node values. Empty line for empty list.

Output Format

A single line of space-separated integers representing the reversed list.

Examples

Example 1
Input
5 4 3 2 1
Output
1 2 3 4 5

Reversing the linked list [5->4->3->2->1] gives [1->2->3->4->5].

Example 2
Input
10
Output
10

Single element list remains unchanged.

Constraints

  • 0 ≤ list length ≤ 5000
  • -10^4 ≤ node value ≤ 10^4
  • Empty list is represented by empty input
Loading interactive editor…