33 lines
968 B
Java
33 lines
968 B
Java
|
package com.zerroi.leetcode.Three26;
|
||
|
|
||
|
public class Partition {
|
||
|
public static void main(String[] args) {
|
||
|
ListNode head = ListNode.constructLinkedList(new int[]{1, 4, 3, 2, 5, 2});
|
||
|
SolutionSecond solutionSecond = new SolutionSecond();
|
||
|
ListNode res = solutionSecond.partition(head, 3);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class SolutionSecond {
|
||
|
public ListNode partition(ListNode head, int x) {
|
||
|
if (head == null) return null;
|
||
|
ListNode dummy1 = new ListNode(-1);
|
||
|
ListNode cur1 = dummy1;
|
||
|
ListNode dummy2 = new ListNode(-1);
|
||
|
ListNode cur2 = dummy2;
|
||
|
while (head != null) {
|
||
|
if (head.val < x) {
|
||
|
cur1.next = new ListNode(head.val);
|
||
|
cur1 = cur1.next;
|
||
|
} else {
|
||
|
cur2.next = new ListNode(head.val);
|
||
|
cur2 = cur2.next;
|
||
|
}
|
||
|
head = head.next;
|
||
|
}
|
||
|
cur1.next = dummy2.next;
|
||
|
|
||
|
return dummy1.next;
|
||
|
}
|
||
|
}
|