Tags: "leetcode", "linked-list", access_time 1-min read

Edit this post on Github

Remove Duplicates From Sorted List

Created: September 16, 2018 by [lek-tin]

Last updated: September 16, 2018

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

Solution

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        curr = head
        while curr != None and curr.next != None:
            if curr.val == curr.next.val:
                curr.next = curr.next.next
            else:
                curr = curr.next
        return head