Kata: https://www.codewars.com/kata/linked-lists-remove-duplicates/train/python
Description
Write a RemoveDuplicates() function which takes a list sorted in increasing order and deletes any duplicate nodes from the list. Ideally, the list should only be traversed once. The head of the resulting list should be returned.
var list = 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5 -> null removeDuplicates(list) === 1 -> 2 -> 3 -> 4 -> 5 -> null
If the passed in list is null/None/nil, simply return null.
Note: Your solution is expected to work on long lists. Recursive solutions may fail due to stack size limitations.
My Solution
class Node(object):
def __init__(self, data, nxt=None):
self.data = data
self.next = nxt
def remove_duplicates(head):
node = Node(None, head)
st = set()
while node.next:
if node.next.data in st:
tmp = node.next
node.next = node.next.next
del tmp
else:
st.add(node.next.data)
node = node.next
return head
I am interested in performance and adhering to best practices.