Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 2095. Delete the Middle Node of a Linked List in F#

Shohei Yoshida's photo
Shohei Yoshida
·Oct 14, 2022·

1 min read

URL

leetcode.com/problems/delete-the-middle-nod..

Code

github.com/syohex/dotnet-study/blob/master/..

type MyList =
    | Leaf
    | Node of int * MyList

let deleteMiddle (head: MyList) : MyList =
    let rec deleteMiddle' node i =
        match node with
        | Leaf -> i, Leaf
        | Node (v, next) ->
            let len, lst = deleteMiddle' next (i + 1)

            if i = (len / 2) then
                len, next
            else
                len, Node(v, lst)

    deleteMiddle' head 0 |> snd
 
Share this