725. Split Linked List in Parts Medium

@problem@discussion
#Linked List



1/**
2 * [725] Split Linked List in Parts
3 *
4 * Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.
5 * The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.
6 * The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.
7 * Return an array of the k parts.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg" style="width: 400px; height: 134px;" />
11 * Input: head = [1,2,3], k = 5
12 * Output: [[1],[2],[3],[],[]]
13 * Explanation:
14 * The first element output[0] has output[0].val = 1, output[0].next = null.
15 * The last element output[4] is null, but its string representation as a ListNode is [].
16 * 
17 * Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg" style="width: 600px; height: 60px;" />
19 * Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
20 * Output: [[1,2,3,4],[5,6,7],[8,9,10]]
21 * Explanation:
22 * The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
23 * 
24 *  
25 * Constraints:
26 * 
27 * 	The number of nodes in the list is in the range [0, 1000].
28 * 	0 <= Node.val <= 1000
29 * 	1 <= k <= 50
30 * 
31 */
32pub struct Solution {}
33use crate::util::linked_list::{ListNode, to_list};
34
35// problem: https://leetcode.com/problems/split-linked-list-in-parts/
36// discuss: https://leetcode.com/problems/split-linked-list-in-parts/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40// Definition for singly-linked list.
41// #[derive(PartialEq, Eq, Clone, Debug)]
42// pub struct ListNode {
43//   pub val: i32,
44//   pub next: Option<Box<ListNode>>
45// }
46// 
47// impl ListNode {
48//   #[inline]
49//   fn new(val: i32) -> Self {
50//     ListNode {
51//       next: None,
52//       val
53//     }
54//   }
55// }
56impl Solution {
57    pub fn split_list_to_parts(head: Option<Box<ListNode>>, k: i32) -> Vec<Option<Box<ListNode>>> {
58        vec![]
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_725() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.