Order the steps
Order one step of the loop.
- Save the link after the current one
- Move the current pointer to the saved link
- Point the current link at the one behind it
- Move the 'behind' pointer to the current link
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A C D B
- Save the link after the current one Otherwise the rest of the chain is lost once the arrow moves.
- Point the current link at the one behind it This flips one arrow.
- Move the 'behind' pointer to the current link It becomes the head of the flipped part.
- Move the current pointer to the saved link Continue with the untouched part.
Steps come from running the Python solution.
Text version
Four links:
Links [1, 2, 3, 4].
- head = 0, prev = 0, cur = 1. Point 1 back at None
- head = 1, prev = 1, cur = 2. Point 2 back at 1
- head = 2, prev = 2, cur = 3. Point 3 back at 2
- head = 3, prev = 3, cur = end. Point 4 back at 3
Result: [4, 3, 2, 1]
Two links:
Links [8, 5].
- head = 0, prev = 0, cur = 1. Point 8 back at None
- head = 1, prev = 1, cur = end. Point 5 back at 8
Result: [5, 8]