I am practicing a problem in "Cracking the Coding Interview". The problem is to remove duplicates in a linked list without the use of a buffer. I interpreted this as not using a list or any large data structure such as a hash to store unique nodes.
My algorithm is inefficient I think. It iterates an anchor across the linked list. From that anchor, a second sub-iteration occurs which removes any nodes that is the same as the anchor. Thus, there are a total of two loops. I think the time complexity is either O(n^2) or O(nlogn)
Any better algorithms are welcome. Since I am a novice in Python, please give me suggestions on my coding style.
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
def getData(self):
return self.data
def setNext(self, node):
self.next = node
def getNext(self):
return self.next
class LinkedList(object):
def __init__(self, dataList):
assert len(dataList) > 0
self.head = Node(dataList[0])
iterNode = self.head
for i in range(1, len(dataList)):
iterNode.setNext(Node(dataList[i]))
iterNode = iterNode.getNext()
iterNode.setNext(None)
def printList(self):
iterNode = self.head
while iterNode is not None:
print(iterNode.getData())
iterNode = iterNode.getNext()
def removeDuplicates(self):
assert self.head is not None
anchor = self.head
while anchor is not None:
iterator = anchor
while iterator is not None:
prev = iterator
iterator = iterator.getNext()
if iterator is None:
break
if iterator.getData() == anchor.getData():
next = iterator.getNext()
prev.setNext(next)
anchor = anchor.getNext()
dataList = ["hello", "world", "people", "hello", "hi", "hi"]
linkedList = LinkedList(dataList)
linkedList.printList()
linkedList.removeDuplicates()
print("\n")
linkedList.printList()
Output:
hello
world
people
hello
hi
hi
hello
world
people
hi