Partition List
来源:互联网 发布:先导化合物的优化方法 编辑:程序博客网 时间:2024/06/08 09:17
public ListNode partition(ListNode head, int x) { // Start typing your Java solution below // DO NOT write main() function if(head == null) return null; ListNode tmp = new ListNode(-1); tmp.next = head; ListNode current = head; ListNode previous = tmp; ListNode next = null; while(current.next != null && current.val < x) { previous = current; current = current.next; } while(current.next != null) { next = current.next; if(next.val < x) { current.next = next.next; next.next = previous.next; previous.next = next; previous = next; }else { current = current.next; } } head = tmp.next; tmp.next = null; return head; }