public static ListNode partition(ListNode head, int x) {
if(head == null) {
return head;
}
ListNode mhead = null,shead = null,scur =null, mcur = null,cur = head;
while(cur != null) {
if(cur.val < x) {
if(mhead == null) {
mhead = cur;
mcur = cur;
}else {
mcur.next = cur;
mcur = cur;
}
}else {
if(shead == null) {
shead = cur;
scur = cur;
}else {
scur.next = cur;
scur = cur;
}
}
cur = cur.next;
}
if(scur != null) {
scur.next =null;
}
if(mcur != null) {
mcur.next = shead;
}else {
mhead = shead;
}
return mhead;
}
86. Partition List
最新推荐文章于 2024-08-06 17:50:14 发布