您的位置:首页 > 其它

Partition List

2013-08-22 05:28 239 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: