解题思路
构建两条新链表
1.一条用来存小于x的数,一条用来存小于等于x的数
2.将两条链表链接
代码
/**
* 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 node1 = new ListNode();
ListNode node2 = new ListNode();
ListNode n1 = node1;
ListNode n2 = node2;
while (head!=null){
if (head.val<x){
n1.next=new ListNode(head.val);
n1=n1.next;
}else {
n2.next=new ListNode(head.val);
n2=n2.next;
}
head=head.next;
}
n1.next=node2.next;
return node1.next;
}
}