Back to the journalNOTES BY FAJAR
Algorithms2 min read

Introduction to In-Place Manipulation of a Linked List

Reverse a linked list by changing its links while preserving access to the remaining nodes.

Reversing a linked list does not require a second list. The nodes can stay where they are while their links change. That in place technique opened up a different way of thinking about linked lists for me.

In place linked lists

In place modification uses O(1) extra space beyond the input. For a linked list, that means updating the next pointers without allocating replacement nodes. When I first tried to reverse a linked list, I copied the values and built a new one:

javascript
function reverseLinkedListNaive(head) {
  const values = [];
  let current = head;

  // Collect all values
  while (current) {
    values.push(current.val);
    current = current.next;
  }

  // Create new list in reverse
  let newHead = null;
  for (let i = 0; i < values.length; i++) {
    newHead = new ListNode(values[i], newHead);
  }

  return newHead;
}

The loop reads values in their original order and inserts each new node at the head. This reverses the list. The values array and replacement nodes use O(n) extra space. Outside the problem platform, this example needs a ListNode(value, next) constructor.

I do not need new nodes. I keep references to the previous node, the current node, and the next node. These references preserve access to the list when a link changes.

The in place implementation is:

javascript
function reverseLinkedList(head) {
  let prev = null; // Previous node (starts as null)
  let current = head; // Current node we're processing

  while (current !== null) {
    let next = current.next; // Save next node
    current.next = prev; // Reverse the link!
    prev = current; // Move prev forward
    current = next; // Move current forward
  }

  return prev; // prev is now the new head
}

Each loop saves the next node before changing current.next. It then sets current.next to prev and advances prev to the current node. The loop continues from the saved next node. By the time current is null, prev points to the new head.

Complexity and applications

The list is traversed once, so the time complexity is O(n). The algorithm keeps three references and uses O(1) extra space.

I learned that the same kind of link rewiring appears in:

  • File system management: Rearranging directory structures
  • Memory management: Optimizing memory block organization
  • Compiler optimizations: Restructuring code representations

In place manipulation changes relationships rather than copying data. Tracking the previous, current, and next nodes is enough to reverse a list with linear time and constant extra space. Learning this changed how I approach linked list problems: I now look for safe rewrites of the existing links first.

FILED UNDER

THANKS FOR READING

Did this resonate?

A reaction or a conversation is always welcome.

Loading reactions…

Pass it along

Loading comments...

Related posts

All writing