-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (48 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function(head, x) {
let pre = head;
if(!pre) return head;
let flag = true;
let first = pre = new ListNode(-1);
pre.next = head;
while(pre.next && flag){
if(pre.next.val >= x){
flag = false;
}else{
pre = pre.next;
}
}
if(flag){
return head;
}
find();
return first.next;
function find(){
let node = pre;
let next = node.next;
while(next){
if(next.val < x){
let t = next.next;
next.next = pre.next;
pre.next = next;
next = t;
node.next = t;
pre = pre.next;
}else{
node = next;
next = next.next;
}
}
}
};