LeetCode 86. Partition List in F#

URL

Partition List - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202308/partition_list/main.fsx

type ListNode =
    | Leaf
    | Node of int * ListNode

let partition (head: ListNode) (x: int) : ListNode =
    let rec listToListNode nums =
        match nums with
        | [] -> Leaf
        | h :: t -> Node(h, listToListNode t)

    let rec partition' node x acc1 acc2 =
        match node with
        | Leaf -> listToListNode ((List.rev acc1) @ (List.rev acc2))
        | Node(v, next) ->
            if v < x then
                partition' next x (v :: acc1) acc2
            else
                partition' next x acc1 (v :: acc2)

    partition' head x [] []