【LeetCode刷题-链表】--86.分隔链表
86.分隔链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode small = new ListNode(0);
ListNode large = new ListNode(0);
ListNode p = small;
ListNode q = large;
while(head != null){
if(head.val < x){
p.next = head;
p = p.next;
}else{
q.next = head;
q = q.next;
}
head = head.next;
}
q.next = null;
p.next = large.next;
return small.next;
}
}