URL
https://leetcode.com/problems/split-linked-list-in-parts/description/?envType=daily-question&envId=2024-09-08
Code
https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202409/split_linked_list_in_parts/main.fsx
type MyList =
| Nil
| Node of int * MyList
let rec listToMyList (nums: int list) : MyList =
match nums with
| [] -> Nil
| h :: t -> Node(h, listToMyList t)
let myListLength (lst: MyList) : int =
let rec myListLength' lst acc =
match lst with
| Nil -> acc
| Node(_, next) -> myListLength' next (1 + acc)
myListLength' lst 0
let splitListToParts (head: MyList) (k: int) : MyList list =
let rec splitListToParts' node count n m acc ret =
match node with
| Nil ->
let nulls = k - List.length ret
let ret = seq { 1..nulls } |> Seq.fold (fun acc _ -> Nil :: acc) ret
List.rev ret
| Node(v, next) ->
if m > 0 && count = n + 1 then
let lst = listToMyList (List.rev (v :: acc))
splitListToParts' next 1 n (m - 1) [] (lst :: ret)
elif m = 0 && count = n then
let lst = listToMyList (List.rev (v :: acc))
splitListToParts' next 1 n 0 [] (lst :: ret)
else
splitListToParts' next (count + 1) n m (v :: acc) ret
let len = myListLength head
let n, m = len / k, len % k
splitListToParts' head 1 n m [] []