Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 858 Bytes

83-Remove-Duplicates-from-Sorted-List.md

File metadata and controls

41 lines (33 loc) · 858 Bytes

83. Remove Duplicates from Sorted List

2017-04-06

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3.

Solution

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.next = nil
 *     }
 * }
 */
class Solution {
    func deleteDuplicates(_ head: ListNode?) -> ListNode? {
        guard var current = head else { return head }
        while let next = current.next {
            if current.val == next.val {
                current.next = next.next
            } else {
                current = next
            }
        }
        return head
    }
}