Skip to content

visual walkthrough

Odd Even Linked List

MediumPointer PartitioningReported at: MetaAmazonAdobe+1

Partitions nodes into two chains in place.

Solve on LeetCode

The idea

Positions alternate odd, even, odd, even. So the odd nodes form a chain where each links two ahead, and so do the even nodes.

Build both chains at once by relinking, then hang the even chain off the end of the odd one.

Complexity

approachtimespace
Copy into two arraysO(n)O(n)
Two chainsO(n)O(1)

More walkthroughs