From the course: Python Data Structures: Linked Lists

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Removing duplicates

Solution: Removing duplicates - Python Tutorial

From the course: Python Data Structures: Linked Lists

Solution: Removing duplicates

(upbeat music) - [Instructor] All right. Hopefully this challenge wasn't too crazy. How are we feeling? Everyone good? Remember, if you're feeling stuck with any of these challenges, just start writing. All of these lists have a print method. Try something, print it out, see what happens. Keep going. So I'll show you what I did. Here we have the remove_duplicates function. It takes in a linked list. We're going to start with a very familiar pattern, current = ll.head. Now the question is, what do we put into our while loop to keep us moving ahead? Well, what I want to do is look ahead at the data in the next node. And if the data in the next node equals the data in my current node, then I'm going to delete the next node. Remember, in order to delete a node in a linked list, you need to currently be on the previous node. So we want to be looking ahead. So in this while loop here, I'm going to look ahead and check current.next. So while there's a next node, keep going, then if the data…

Contents