# Partition List Problem Solution

Partition List Problem: 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]```

## Problem Solution In Python

``````class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
dummy1 = ListNode(0)
dummy2 = ListNode(0)
else:
return dummy1.next
``````

## Problem Solution In Java

``````public ListNode partition(ListNode head, int x) {
ListNode first=null;
ListNode second=null;
while(cur!=null){
if(cur.val<x){
else {first.next=cur; first=first.next;}
}
else{
else {second.next=cur; second=second.next;}
}
cur=cur.next;
}

if(first==null) {
second.next=null;
}
else if(second==null){

}else{
second.next=null;
}

}
``````

## Problem Solution In C++

``````ListNode* partition(ListNode* head, int target) {
ListNode* fake_head = new ListNode(target - 1);
while (current) {
if (current->val < target) {
if (previous->val < target) {
previous = current;
} else {
previous->next = current->next;
current->next = partition->next;
partition->next = current;
}
partition = current;
} else {
previous = current;
}
current = previous->next;
}
}
``````

## Problem Solution In C

``````struct ListNode* partition(struct ListNode* head, int x){
struct ListNode *p, *q, node, node2;
q = &node;
p = &node2;
while(q->next){
if(q->next->val >= x){
p->next = q->next;
p = p->next;
q->next = p->next;
}
else q = q->next;
}
p->next = NULL;
q->next = node2.next;
return node.next;
}``````