Reversing links without losing your place

An editing application stores four pages in a doubly linked list. Initially, head references A, tail references D, A.prev is null, and D.next is null.


You need an in-place reversal: reverse the list by changing links in its existing nodes, without allocating nodes or moving their stored page data. Only head and tail provide persistent endpoint references.


Four nodes have next links directed from A toward D and prev links directed from D toward A.


Arrange the stages into the correct execution order. Some stages run once for each node. Exchanging two fields means swapping their values without losing either value.

  1. 1.

    Once current is null, set head = oldTail and tail = oldHead.

  2. 2.

    Save savedNext = current.next before changing either link in the current node.

  3. 3.

    Set current = savedNext; if current is not null, return to the start of the loop.

  4. 4.

    Save oldHead = head and oldTail = tail, then set current = oldHead.

  5. 5.

    Exchange current.next and current.prev, leaving savedNext unchanged.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Doubly Linked Lists Questions: