44 lines
1.0 KiB
Java
44 lines
1.0 KiB
Java
/*
|
|
* @lc app=leetcode id=86 lang=java
|
|
*
|
|
* [86] Partition List
|
|
*/
|
|
|
|
// @lc code=start
|
|
/**
|
|
* 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 tf = new ListNode(0),
|
|
tb = new ListNode(0);
|
|
tf.next = null;
|
|
tb.next = null;
|
|
ListNode cur = head, tailf = tf, tailb = tb;
|
|
while(cur != null) {
|
|
if(cur.val < x) {
|
|
tailf.next = cur;
|
|
cur = cur.next;
|
|
tailf = tailf.next;
|
|
tailf.next = null;
|
|
} else {
|
|
tailb.next = cur;
|
|
cur = cur.next;
|
|
tailb = tailb.next;
|
|
tailb.next = null;
|
|
}
|
|
}
|
|
tailf.next = tb.next;
|
|
return tf.next;
|
|
}
|
|
}
|
|
// @lc code=end
|
|
|