A Swift Solution


  • 0
    T
        func deleteDuplicates(head: ListNode?) -> ListNode? {
            
            if head == nil || head?.next == nil {
                return head
            }
            var pre = head
            var pres = head!.next
            while pres != nil {
                if pre!.val == pres!.val { // 重复,删除节点
                    pres = pres!.next
                    pre!.next = pres
                }else {
                    pre = pres
                    pres = pres!.next
                }
            }
            return head
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.