题目:
我的解法:创建两个新链表,合并
/**
* 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 small = new ListNode(-1);
ListNode big = new ListNode(-1);
ListNode dummy_small = small;
ListNode dummy_big = big;
while(head!=null){
if(head.val < x){
small.next = new ListNode(head.val);
small = small.next;
}else{
big.next = new ListNode(head.val);
big = big.next;
}
head = head.next;
}
small.next = dummy_big.next;
return dummy_small.next;
}
}
官方题解: