DSA Factory
Free Linked lists lessonsLinked lists · Stage 3 · Merging and splitting · Step 2

Split into two chains and rejoin

Partition a list around a value by sending each node to one of two growing chains, then joining the chains end to end. About 14 minutes.

Two chains, one pass

To put every node smaller than x in front, keep the original order, and not copy anything, deal the nodes into two piles as you walk. Each pile is a chain with its own dummy node and tail.

A node smaller than x goes to the end of the small chain. Every other node goes to the end of the other chain. Because you always append, each chain keeps the order the nodes had in the input.

Two dummies: mean no special first case.
Always append: so order is kept.
In code
tail.next = node
tail = node
tail.next = None
Split 2, 6, 4, 1, 7, 3 around 4.
2
0
6
1
4
2
1
3
7
4
3
5
smallrest

2 is below 4, so it joins the small chain: 2.

Move 1 of 6

Join and cut

After the walk, link the end of the small chain to the first real node of the other chain, which is the dummy's next. Return the small chain's dummy next.

One trap: the last node of the other chain may still point at some node in the input. Set its next to nothing before you finish, or the joined list can circle back on itself.

Cut the last node: so it points at nothing.
Join: small tail to the other chain's start.
In code
tail1.next = head2   # glue the second on
Quick check

Why must the last node of the second chain be cut off before returning?

  1. AIt may still point at a node already placed, forming a loop
  2. BTo keep the chain sorted
  3. CTo free memory
Show the answer

It may still point at a node already placed, forming a loop. Nodes keep their old links until you change them.

Your problem

Partition list

Given the head of a linked list and a value x, rearrange the list so that every node with a value less than x comes before every node with a value greater than or equal to x. Preserve the original relative order of the nodes within each of the two groups. Return the head.

Example
head = [1, 4, 3, 2, 5, 2], x = 3 → [1, 2, 2, 4, 3, 5]

0 ≤ number of nodes ≤ 5,000 · -1,000,000 ≤ node.val, x ≤ 1,000,000

Solve it in your browserFree account, no card. Python, C++, Java or JavaScript, with hints if you get stuck, and your progress is saved.
Sign up free to solve