-
Notifications
You must be signed in to change notification settings - Fork 0
/
86.分隔链表.java
42 lines (36 loc) · 872 Bytes
/
86.分隔链表.java
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
/*
* @lc app=leetcode.cn id=86 lang=java
*
* [86] 分隔链表
*/
// @lc code=start
/**
* 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) {
if(head==null||head.next==null) return head;
ListNode little = new ListNode(0);
ListNode big = new ListNode(0);
ListNode cur = head,cur_l = little,cur_b = big;
while(cur!=null){
if(cur.val < x){
cur_l.next = cur;
cur_l = cur;
}else {
cur_b.next = cur;
cur_b = cur;
}
cur = cur.next;
}
cur_l.next = big.next;
cur_b.next = null;
return little.next;
}
}
// @lc code=end