O(n)
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if head is None:
return head
walker = head
d = {}
if walker is not None:
d[walker.val] = 1
while walker is not None and walker.next is not None:
if walker.next.val in d:
walker.next = walker.next.next
else:
d[walker.next.val] = 1
walker = walker.next
return head
def main():
s = Solution()
l = [1,1,1]
l = insert(l)
disp(l)
l = s.deleteDuplicates(l)
disp(l)
def insert(nums):
l = ListNode(nums[0])
walker = l
for index in range(1, len(nums)):
walker.next = ListNode(nums[index])
walker = walker.next
return l
def disp(l):
walker = l
while walker is not None:
print(walker.val, end = ' -> ')
walker = walker.next
print('None')
if __name__ == '__main__':
main()