LINK-P3

Reverse a section

Practice 3 of 6Linked lists8 tests

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.
  • left is at least 1, right is at least left, and right is 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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

8 tests, not run yet
Test results
#TestResultReads · time
1Middle partNot run
2From the headNot run
3To the endNot run
4Whole listNot run
5left equals rightNot run
6One nodeNot run
7Same nodesNot run
8100,000 nodesNot run