Given the head of 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.

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Input: head = [2,1], x = 2
Output: [1,2]
From: LeetCode
Link: 86. Partition List
The main idea behind the code is to maintain two separate linked lists:
The code does the following:
Initialization
Traversal
Concatenation
Cleanup
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* partition(struct ListNode* head, int x) {
// Initialize two new dummy nodes to serve as the starting points for the two new lists.
struct ListNode *smallerHead = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *greaterHead = (struct ListNode *)malloc(sizeof(struct ListNode));
smallerHead->val = 0;
greaterHead->val = 0;
smallerHead->next = NULL;
greaterHead->next = NULL;
struct ListNode *smaller = smallerHead;
struct ListNode *greater = greaterHead;
// Traverse the original list
while (head != NULL) {
if (head->val < x) {
smaller->next = head;
smaller = smaller->next;
} else {
greater->next = head;
greater = greater->next;
}
head = head->next;
}
// Connect the two lists
smaller->next = greaterHead->next;
greater->next = NULL;
// The new head of the list is the node following the smaller dummy node.
struct ListNode *newHead = smallerHead->next;
// Free the dummy nodes
free(smallerHead);
free(greaterHead);
return newHead;
}