See more solutions https://jinblack.com/leetcode-swift/

func deleteDuplicates(_ head: ListNode?) -> ListNode? {
    var f: ListNode?
    var l: ListNode?
    var v: Int?
    var node: ListNode? = head
    while node != nil {
        if node?.val == v {
            node = node?.next
        } else if node?.val != v && node?.val != node?.next?.val {
            if f == nil {
                f = node
            } else {
                l?.next = node
            }
            l = node
            v = node?.val
            node = node?.next
        } else {
            v = node?.val
            node = node?.next?.next
        }
    }
    l?.next = nil
    return f
}

Runtime: 32 ms, faster than 100.00% of Swift online submissions for Remove Duplicates from Sorted List II.
Memory Usage: 18.8 MB, less than 56.00% of Swift online submissions for Remove Duplicates from Sorted List II.