Solution Review: Remove Duplicates
This lesson contains the solution review for the challenge of removing duplicates from a doubly linked list.
We'll cover the following...
In this lesson, we consider how to remove duplicates from a doubly linked list.
Implementation
Check out the code below:
Press + to interact
def remove_duplicates(self):cur = self.headseen = dict()while cur:if cur.data not in seen:seen[cur.data] = 1cur = cur.nextelse:nxt = cur.nextself.delete_node(cur)cur = nxt
Explanation
In the method remove_duplicates
, we will keep track of the duplicates using a Python dictionary which we declare on line 3. cur
is set to self.head
on line 2 to help us traverse ...