Two Sum @ LeetCode (Python)
Reverse Integer (Python)

Remove Duplicates from Sorted List (Python)

kitt posted @ 2014年1月29日 18:27 in LeetCode , 1613 阅读

注意head == None的情况,没用到Sorted这个特性,应该有占空间更少的解法。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @return a ListNode
    def deleteDuplicates(self, head):
        if head == None:
            return None
        num = []
        cur = head
        while cur != None:
            if cur.val not in num:
                num.append(cur.val)
            cur = cur.next
        new_head = ListNode(num[0])
        cur = new_head
        for i in num[1:]:
            cur.next = ListNode(i)
            cur = cur.next
        return new_head

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter