美文网首页
LeetCode 83 删除排序链表中的重复元素

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

作者: 麦兜儿流浪记 | 来源:发表于2019-08-03 18:41 被阅读0次

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

示例 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 删除排序链表中的重复元素

      本文链接:https://www.haomeiwen.com/subject/fccsdctx.html