82. Remove Duplicates from Sorted List II

来源:互联网 发布:淘宝质量好的女鞋店铺 编辑:程序博客网 时间:2024/05/20 14:16

题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3. 

代码:

class Solution(object):    def deleteDuplicates(self, head):        """        :type head: ListNode        :rtype: ListNode        """        h = ListNode(float('-inf'))        h.next = head        p = h        while p.next and p.next.next:            if p.next.val == p.next.next.val:                q = p.next.next                while q and p.next.val == q.val:                    q = q.next                p.next = q            else:                p = p.next        return h.next
0 0
原创粉丝点击