需要两个dummy node来构造两个list:
一个是小于target的linked list
另一个是大于等于target的linked list
1 public class Solution { 2 public ListNode partition(ListNode head, int target) { 3 if (head == null) { 4 return head; 5 } 6 // Write your solution here 7 ListNode less = new ListNode(0); 8 ListNode large = new ListNode(0); 9 ListNode lessTail = less; 10 ListNode largeTail = large; 11 12 ListNode cur = head; 13 while (cur != null) { 14 if (cur.value < target) { 15 lessTail.next = cur; 16 lessTail = lessTail.next; 17 } else { 18 largeTail.next = cur; 19 largeTail = largeTail.next; 20 } 21 cur = cur.next; 22 } 23 lessTail.next = large.next; 24 largeTail.next = null; 25 return less.next; 26 } 27 }