83. Remove Duplicates from Sorted List

Source code notebook Author Update time

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

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3
# @lc code=start
using LeetCode

function delete_duplicates!(head::Union{ListNode,Nothing})::Union{ListNode,Nothing}
    cur = head
    while !isnothing(cur)
        node, value = next(cur), val(cur)
        while !isnothing(node) && val(node) == value
            cur.next = node = next(node)
        end
        cur = next(cur)
    end
    return head
end
# @lc code=end
delete_duplicates! (generic function with 1 method)

This page was generated using DemoCards.jl and Literate.jl.