LeetCode 2181. Merge Nodes in Between Zeros in F#

URL

Merge Nodes in Between Zeros - LeetCode

Code

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

type ListNode =
    | Leaf
    | Node of int * ListNode

let mergeNodes (head: ListNode) : ListNode =
    let rec mergeNodes' node acc =
        match node with
        | Leaf -> Leaf
        | Node(v, next) ->
            if v = 0 then
                Node(acc, mergeNodes' next 0)
            else
                mergeNodes' next (acc + v)

    match head with
    | Leaf
    | Node(_, Leaf) -> failwith "never reach here"
    | Node(_, next) -> mergeNodes' next 0