Leetcode 86. Partition List
/**
* 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;
}
}
页:
[1]