LeetCode刷题记107
86. 分隔链表
/**
* 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) {
ListNode s = new ListNode(0);
ListNode b = new ListNode(0);
ListNode sp = s;
ListNode bp = b;
while (head != null) {
if (head.val < x) {
sp.next = head;
head = head.next;
sp = sp.next;
sp.next = null;
} else {
bp.next = head;
head = head.next;
bp = bp.next;
bp.next = null;
}
}
sp.next = b.next;
return s.next;
}
}
4/5
107/150