给你一个链表的头节点 head 和一个特定值 **x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

示例 1:

https://assets.leetcode.com/uploads/2021/01/04/partition.jpg

输入:head = [1,4,3,2,5,2], x = 3
输出:[1,2,2,4,3,5]

示例 2:

输入:head = [2,1], x = 2
输出:[1,2]

提示:

创建两个虚拟节点,一条保存所有小于val 的node, 一条保存大于or 等于Node 的节点,最后一步将两条链拼接,first.next = second.next (第一个值时dummy)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode firstPart = new ListNode(-1);
        ListNode result = firstPart;
        ListNode second = new ListNode (-2);
        ListNode secondPart = second;
        while(head != null ){
            if(head.val < x){
                ListNode temp= new ListNode(head.val);
                firstPart.next = temp;
                firstPart = firstPart.next;
            }
            else{
                ListNode temp= new ListNode(head.val);
                secondPart.next = temp;
                secondPart = secondPart.next;
            }
            head = head.next;
        }
         firstPart.next = second.next ;
         return result.next;
    }
}