LeetCode 83 删除排序链表中的重复元素

题目:
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2
示例 2:

输入: 1->1->2->3->3
输出: 1->2->3

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head == None or head.next == None:
            return head
        cur = head
        while cur.next:
            if cur.val == cur.next.val:
                if cur.next.next == None:
                    cur.next = None
                else:
                    cur.next = cur.next.next
            else:
                cur = cur.next

        return head

你可能感兴趣的:(LeetCode 83 删除排序链表中的重复元素)