Remove Duplicates from Sorted List

Problem: Remove Duplicates from Sorted List

Since it's a sorted list, duplicates should be next to each other. So we can just check the next element to avoid duplicates.

Code in Python:

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        cur = head
        while cur:
            if not cur.next:
                break
            if cur.val == cur.next.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head

results matching ""

    No results matching ""