This code solves the Partition List problem.
1 2 3 4 5 6 7 8 9 10Given a linked list and a value *x*, partition it such that all nodes less than *x* come before nodes greater than or equal to *x*. You should preserve the original relative order of the nodes in each of the two partitions. **Example:** ``` Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5 ```
The following is an explanation of my implemention of the two-pointer solution given by LeetCode. My initial attempt (at the bottom of this post) only worked for lists of length greater than 3 and didn’t pass the submission after multiple revisions, and it relied on 6 pointers, rather than 2.
LeetCode’s Two-Pointer Solution¶
For a more in depth explanation, check the solution here.
The trick I learned from this problem is that lists can be grown in the direction they point without loosing the head, if you save it at the beginning as is done on lines 20 and 21 below.
After all is said and done, a list that starts out with the following arrangement:
1 |
|
Will have a pointer arrangement that looks like this:
1 2 3 4 5 6 7 8 9 10 11 |
|
After this, gtx_head
can be stiched to the back of ltx_head
to return the final answer.
Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
My Original Solution¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|