The problem
Reverse only the part of the list from position left to position right, both included. Positions count from 1. The nodes before and after that part stay where they are. Return the head of the whole list.
list 1, 2, 3, 4, 5, left 2, right 4 -> 1, 4, 3, 2, 5
list 1, 2, 3, 4, 5, left 1, right 3 -> 3, 2, 1, 4, 5
The tests build the list from nodes with a value and a next, pass you the head, then walk from the head you return, following next, and compare the values they find. An empty list is None.
Relink the nodes you are given; do not copy values into new nodes.
Limits
- Up to 100,000 nodes.
leftis at least 1,rightis at leastleft, andrightis at most the number of nodes.
Builds on LINK-4 Reversing.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | Middle part | Not run | |
| 2 | From the head | Not run | |
| 3 | To the end | Not run | |
| 4 | Whole list | Not run | |
| 5 | left equals right | Not run | |
| 6 | One node | Not run | |
| 7 | Same nodes | Not run | |
| 8 | 100,000 nodes | Not run |