How do you partition a list around a value x, such that all nodes less than x come before all nodes greater than or equal to x?

Well, there are some solutions possible. The solution, I came up with, is a bit convoluted but let me tell the idea behind it. You want to track the following:

  • Two pointers to remember the beginning of the lower and higher series each

  • One pointer (current) to iterate through the Linked List

  • The list may itself start with higher or lower value compared to the middleValue. Thus we need to remember the beginning of the lower series (lowerSeries) as this is what we will send back

Now that we have this out of the way, let’s look at the code:

Code

As usual the code is available here:

https://github.com/abhi1010/Algorithms