[LeetCode]Partition List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode partition(ListNode head, int x) { ListNode small = new ListNode(0); ListNode big = new ListNode(0); ListNode p = head; ListNode s = small; ListNode b = big; while (p != null) { if (p.val < x) { s.next = p; s = s.next; p = p.next; s.next = null; } else { b.next = p; b = b.next; p = p.next; b.next = null; } } s.next = big.next; return small.next; } }