/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
#include <cstddef>
class Partition 
{
  public:
    ListNode* partition(ListNode* pHead, int x) 
    {
        // write code here
        struct ListNode* head1, *tail1, *head2, *tail2;
        head1=tail1=(struct ListNode*)malloc(sizeof(struct ListNode));
        head2=tail2=(struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* cur = pHead;
        while (cur) 
        {
            if (cur->val >= x) 
            {
                tail2->next = cur;
                tail2 = tail2->next;
            } 
            else 
            {
                tail1->next = cur;
                tail1 = tail1->next;
            }
            cur = cur->next;

        }
        tail2->next = NULL;
        tail1->next = head2->next;
        pHead=head1->next;
        free(head1);
        free(head2);
        return pHead;

    }
};