Turn a chain of links around

Linked lists Junior Python 3.13+ Kotlin 2.4+ Reviewed By Best Latest Apps

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

  1. Save the link after the current one Otherwise the rest of the chain is lost once the arrow moves.
  2. Point the current link at the one behind it This flips one arrow.
  3. Move the 'behind' pointer to the current link It becomes the head of the flipped part.
  4. Move the current pointer to the saved link Continue with the untouched part.
A chain of links being turned around one arrow at a time, with the back and current pointers shown at every step, for four links and for two links.

Steps come from running the Python solution.

Text version

Four links:

Links [1, 2, 3, 4].

  1. head = 0, prev = 0, cur = 1. Point 1 back at None
  2. head = 1, prev = 1, cur = 2. Point 2 back at 1
  3. head = 2, prev = 2, cur = 3. Point 3 back at 2
  4. head = 3, prev = 3, cur = end. Point 4 back at 3

Result: [4, 3, 2, 1]

Two links:

Links [8, 5].

  1. head = 0, prev = 0, cur = 1. Point 8 back at None
  2. head = 1, prev = 1, cur = end. Point 5 back at 8

Result: [5, 8]

30-second answer

Walk the chain with three names: the link behind you, the current link and the one ahead. Save the link ahead, point the current link back, then step everyone forward. When you fall off the end, the link behind you is the new head. O(n) time, O(1) memory.

Practise saying it · 45 s. No recording, just a timer

Explain turning a chain around in place.

Say your answer out loud, then check which of these points you covered:

  • Save next first
  • Flip one arrow per step
  • O(n) time, O(1) memory

Deep dive

This is the template for every pointer-rewiring question: save, rewire, step.

A recursive version is shorter but uses O(n) stack and breaks on long chains in Python.

Turning around only part of a chain uses the same loop with a counter.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def turn_around(head: Link | None) -> Link | None:
    back: Link | None = None
    here = head
    while here is not None:
        ahead = here.next  # save it before the arrow moves
        here.next = back
        back, here = here, ahead
    return back

Kotlin Ran on Kotlin 2.4.21 · JDK 21.0.12.1
Kotlin 2.4
fun turnAround(head: Link?): Link? {
    var back: Link? = null
    var here = head
    while (here != null) {
        val ahead = here.next // save it before the arrow moves
        here.next = back
        back = here
        here = ahead
    }
    return back
}

Common mistakes

  • Changing here.next before saving it, which loses the rest of the chain.
  • Returning the old head, which is now the tail.

Senior take & follow-ups

Draw the three pointers before you code, and narrate each step.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you turn around only the links from position a to b?

    Show hint

    Walk to a - 1, run the loop b - a + 1 times, then reconnect both ends.

  2. How do you flip it in groups of k?

    Show hint

    Run the same loop on each group and link each group's new tail to the next group's head.

What each operation costs: Linked List · Singly Linked List · Doubly Linked List

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.