ListNode *partition(ListNode *head, int x) {
ListNode* emptyNode = new ListNode (0);
emptyNode->next = head;
ListNode **p = &head, **lt = &emptyNode, **lg = &emptyNode;
while (*p) {
if ((*p)->val < x) {
if(lg == lt) {
lt = p;
lg = p;
}
else {
ListNode* t = *p;
(*lg)->next = (*p)->next;
t->next = (*lt)->next;
(*lt)->next = t;
lt = &(*lt)->next;
}
}
else {
lg = p;
}
p = &(*lg)->next;
}
return emptyNode->next;
}

how about this solution? I hope to simplify this function, but I don't known how to do.