链表
双指针
题目描述
给你一个链表的头节点 head
和一个特定值 x
,请你对链表进行分隔,使得所有 小于 x
的节点都出现在 大于或等于 x
的节点之前。
你应当 保留 两个分区中每个节点的初始相对位置。
示例 1:
输入: head = [1,4,3,2,5,2], x = 3
输出 :[1,2,2,4,3,5]
示例 2:
输入: head = [2,1], x = 2
输出 :[1,2]
提示:
链表中节点的数目在范围 [0, 200]
内
-100 <= Node.val <= 100
-200 <= x <= 200
解法
方法一:模拟
我们创建两个链表,一个存放小于 $x$ 的节点,另一个存放大于等于 $x$ 的节点,之后进行拼接即可。
时间复杂度 $O(n),其中 $n$ 是原链表的长度。空间复杂度 $O(1)$。
Python3 Java C++ Go Rust JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution :
def partition ( self , head : Optional [ ListNode ], x : int ) -> Optional [ ListNode ]:
d1 , d2 = ListNode (), ListNode ()
t1 , t2 = d1 , d2
while head :
if head . val < x :
t1 . next = head
t1 = t1 . next
else :
t2 . next = head
t2 = t2 . next
head = head . next
t1 . next = d2 . next
t2 . next = None
return d1 . next
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 /**
* 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 d1 = new ListNode ();
ListNode d2 = new ListNode ();
ListNode t1 = d1 , t2 = d2 ;
while ( head != null ) {
if ( head . val < x ) {
t1 . next = head ;
t1 = t1 . next ;
} else {
t2 . next = head ;
t2 = t2 . next ;
}
head = head . next ;
}
t1 . next = d2 . next ;
t2 . next = null ;
return d1 . next ;
}
}
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 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public :
ListNode * partition ( ListNode * head , int x ) {
ListNode * d1 = new ListNode ();
ListNode * d2 = new ListNode ();
ListNode * t1 = d1 ;
ListNode * t2 = d2 ;
while ( head ) {
if ( head -> val < x ) {
t1 -> next = head ;
t1 = t1 -> next ;
} else {
t2 -> next = head ;
t2 = t2 -> next ;
}
head = head -> next ;
}
t1 -> next = d2 -> next ;
t2 -> next = nullptr ;
return d1 -> next ;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func partition ( head * ListNode , x int ) * ListNode {
d1 , d2 := & ListNode {}, & ListNode {}
t1 , t2 := d1 , d2
for head != nil {
if head . Val < x {
t1 . Next = head
t1 = t1 . Next
} else {
t2 . Next = head
t2 = t2 . Next
}
head = head . Next
}
t1 . Next = d2 . Next
t2 . Next = nil
return d1 . Next
}
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 // Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn partition ( head : Option < Box < ListNode >> , x : i32 ) -> Option < Box < ListNode >> {
let mut head = head ;
let mut d1 = Some ( Box :: new ( ListNode :: new ( 0 )));
let mut d2 = Some ( Box :: new ( ListNode :: new ( 0 )));
let ( mut t1 , mut t2 ) = ( & mut d1 , & mut d2 );
while let Some ( mut node ) = head {
head = node . next . take ();
if node . val < x {
t1 . as_mut (). unwrap (). next = Some ( node );
t1 = & mut t1 . as_mut (). unwrap (). next ;
} else {
t2 . as_mut (). unwrap (). next = Some ( node );
t2 = & mut t2 . as_mut (). unwrap (). next ;
}
}
t1 . as_mut (). unwrap (). next = d2 . unwrap (). next ;
d1 . unwrap (). next
}
}
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 /**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function ( head , x ) {
const d1 = new ListNode ();
const d2 = new ListNode ();
let t1 = d1 ,
t2 = d2 ;
while ( head ) {
if ( head . val < x ) {
t1 . next = head ;
t1 = t1 . next ;
} else {
t2 . next = head ;
t2 = t2 . next ;
}
head = head . next ;
}
t1 . next = d2 . next ;
t2 . next = null ;
return d1 . next ;
};