370. Reverse a Sub-list from Position M to N

MediumLinked ListLinked ListPointers

Given the head of a singly linked list and two 1-based positions m and n with m <= n, reverse the nodes from position m to n inclusive and return the list. The list is given as an array; return the resulting array. Input: '[list], m, n'.

Input: '[list], m, n'.

Output: Array — the list after reversing the segment.

Examples

Example 1
Input: [1,2,3,4,5], 2, 4
Output: [1,4,3,2,5]
Explanation: Positions 2..4 reversed.
Example 2
Input: [5], 1, 1
Output: [5]
Explanation: Single node.
Example 3
Input: [1,2,3,4,5], 1, 5
Output: [5,4,3,2,1]
Explanation: Whole list reversed.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →