/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode partition(ListNode head, int x) {
// Start typing your Java solution below
// DO NOT write main() function
if(head ==null){
return null;
}
ListNode cur = head;
ListNode pre = head;
ListNode less = null;
ListNode more = null;
boolean isFirstMore = true;
while(cur!=null){
if(cur.val<x){
pre.next=cur.next;
if(more!=null){
cur.next=more;
}
if(less!=null){
less.next=cur;
}else{
head=cur;
}
less=cur;
}else{
if(isFirstMore){
more=cur;
isFirstMore=false;
}
}
pre=cur;
cur=cur.next;
}
return head;
}
}
Partition List
最新推荐文章于 2021-04-06 09:37:45 发布