All problems

Problem 387

Reverse a Sublist

Medium · 20 pts · Linked List

Problem

Reverse the part of a linked list from position L to position R (positions counted from 1, both inclusive) and leave the rest of the list unchanged. Print the resulting list. The challenge on a real linked list is to do this in a single pass: walk to the node before position L, then move each node of the sublist to the front of the sublist one at a time.

Input

First line: N, L and R. Second line: the N node values.

Output

Print the node values of the resulting list from head to tail, separated by spaces, or EMPTY if the list has no nodes.

Constraints

1 <= L <= R <= N <= 500; -500 <= value <= 500

Example input

5 2 4
1 2 3 4 5

Example output

1 4 3 2 5

Explanation

Reversing positions 2 to 4 of 1 2 3 4 5 turns the middle part 2 3 4 into 4 3 2, giving 1 4 3 2 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Split Linked List into Parts Next: Next Smaller Element