LeetCode 2816. Double a Number Represented as a Linked List in F#

URL

Double a Number Represented as a Linked List - LeetCode

Code

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

type MyList =
    | Leaf
    | Node of int * MyList

let doubleIt (head: MyList) : MyList =
    let rec doubleIt' node =
        match node with
        | Leaf -> Leaf, 0
        | Node(v, next) ->
            let lst, carry = doubleIt' next
            let v' = v * 2 + carry
            if v' >= 10 then Node(v' % 10, lst), 1 else Node(v', lst), 0

    let lst, carry = doubleIt' head
    if carry > 0 then Node(1, lst) else lst