Tuesday, August 18, 2015

Leetcode 82. Remove Duplicates from Sorted List II

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

Linked list operation. Using a pile of '.next' is actually more clear than 'p', 'q', 'tmp' or something.

# T:O(n) S:O(1)
class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        dummy = ListNode(0)
        dummy.next = head
        current = dummy
        while current.next:
            next = current.next
            while next.next and next.next.val == next.val:
                next = next.next
            if current.next is not next:
                current.next = next.next
            else:
                current = current.next
        return dummy.next
Run Time: 88 ms

No comments:

Post a Comment