马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
if(head == null){
return null;
}
ListNode less_head = new ListNode(0);
ListNode more_head = new ListNode(0);
ListNode less_ptr = less_head;
ListNode more_ptr = more_head;
while(head != null){
if(head.val >= x){
more_ptr.next = head;
more_ptr = more_ptr.next;
}
else{
less_ptr.next = head;
less_ptr = less_ptr.next;
}
head = head.next;
}
less_ptr.next = more_head.next;
more_ptr.next = null;
return less_head.next;
}
}
|