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.

Searching

Searching

- [Instructor] Earlier I discussed some pros and cons of linked lists versus Python lists, arrays, tuples, et cetera. Well, one thing is for sure, in an unsorted linked list, searching takes the same time as in any other unsorted linear data structure. You just have to go through it one element at a time and check whether you found your data or not. So let's get to it. I wrote some code down here that creates a randomly shuffled linked list with the value zero through nine, perfect for searching. And if we print this out, you can see, there you go. There's our random linked list. So next I'm going to add a search method to the linked list class, and I'm going to do that right above the print method here. Def, search, and we pass in some data that we want to search for. So this is going to return a Boolean, whether the value is found or not. And we're going to use a very similar traversal technique to what we used earlier to print the list. So first we set current equal to self.head…

Contents