# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None:
return None
start = head
prev = head.val
a = {prev:1}
#o = [prev]
head = head.next
while head != None:
if head.val == prev:
a[prev] += 1
else:
prev = head.val
a[prev] = 1
#o.append(prev)
head = head.next
b = ListNode(0)
ans = b
for i in a: # can use for i in o
if a[i] == 1:
c = ListNode(i)
b.next = c
b = b.next
return ans.next
I am trying to remove duplicate items from a sorted linked list, eg.[1,2,3,3,4,4,5,6,6,6]
-> [1,2,5]
. Can someone walk through the code and tell me what will be the final value of a
is for the linked list 2
->1
->None
. It should be {2:1, 1:1}
but answer comes out to be {1:1, 2:1}
...why?