A support tool lists waiting tickets in a linked list. An agent selects tickets from position left to position right (counting from 1) and clicks "reverse selection".
Write reverseStretch(head, left, right) that reverses only the nodes from position left to position right, keeps the rest in place, and returns the head. Do it in one pass if you can.
Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5]
Positions 2 to 4 hold 2, 3, 4, which become 4, 3, 2.
Input: head = [5], left = 1, right = 1 Output: [5]
Reversing one node changes nothing.
1 to 500 nodes.-500 <= node.val <= 5001 <= left <= right <= the number of nodesLists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #240 Reverse a Stretch of the Queue. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.