【LeetCode with Python】 Remove Duplicates from Sorted List II

博客域名: http://www.xnerv.wang
原题页面: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
题目类型:
难度评价:★
本文地址: http://blog.csdn.net/nerv3x3/article/details/38929151

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinct 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:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        if None == head or None == head.next:
            return head

        new_head = ListNode(-1)
        new_head.next = head
        parent = new_head
        cur = head
        while None != cur and None != cur.next:   ### check cur.next None
            if cur.val == cur.next.val:
                val = cur.val
                while None != cur and val == cur.val: ### check cur None
                    cur = cur.next
                parent.next = cur
            else:
                cur = cur.next
                parent = parent.next

        return new_head.next

你可能感兴趣的:(LeetCode,LeetCode,python,python,with)