LeetCode 24. Swap Nodes in Pairs in F#
URL
Swap Nodes in Pairs - LeetCode
Code
type ListNode =
| Leaf
| Node of int * ListNode
let swapPair (head: ListNode) =
let rec swapPair' node =
match node with
| Leaf -> Leaf
| Node(v1, next1) ->
match next1 with
| Leaf -> node
| Node(v2, next2) -> Node(v2, Node(v1, swapPair' next2))
swapPair' head