86. Partition List Medium
1/**
2 * [86] Partition List
3 *
4 * Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
5 * You should preserve the original relative order of the nodes in each of the two partitions.
6 *
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/partition.jpg" style="width: 662px; height: 222px;" />
9 * Input: head = [1,4,3,2,5,2], x = 3
10 * Output: [1,2,2,4,3,5]
11 *
12 * Example 2:
13 *
14 * Input: head = [2,1], x = 2
15 * Output: [1,2]
16 *
17 *
18 * Constraints:
19 *
20 * The number of nodes in the list is in the range [0, 200].
21 * -100 <= Node.val <= 100
22 * -200 <= x <= 200
23 *
24 */
25pub struct Solution {}
26use crate::util::linked_list::{ListNode, to_list};
27
28// problem: https://leetcode.com/problems/partition-list/
29// discuss: https://leetcode.com/problems/partition-list/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33// Definition for singly-linked list.
34// #[derive(PartialEq, Eq, Clone, Debug)]
35// pub struct ListNode {
36// pub val: i32,
37// pub next: Option<Box<ListNode>>
38// }
39//
40// impl ListNode {
41// #[inline]
42// fn new(val: i32) -> Self {
43// ListNode {
44// next: None,
45// val
46// }
47// }
48// }
49impl Solution {
50 pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
51 Some(Box::new(ListNode::new(0)))
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_86() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.