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: Finding a middle ground

Solution: Finding a middle ground - Python Tutorial

From the course: Python Data Structures: Linked Lists

Solution: Finding a middle ground

(upbeat music) - [Instructor] Are you a Linked Lists master? Well, let's not get ahead of ourselves. That was a little fast and slow pointer joke. Get it, like get ahead of yourself? Okay, let's just look at the solution. We have our function get_midpoint. It takes in a linked list object and needs to return the data associated with the midpoint. So with all of these challenges, you really just want to write it out. We know that we need a slow pointer and we know that we need a fast pointer that moves twice as slow as the slow pointer. So we just write it, slow = ll.head, fast = ll.head. Then we're going to need some sort of loop to keep us traversing through the list. And we know that this loop needs to depend on something about the fast pointer. We want to watch the fast pointer and stop when the fast pointer reaches the end of the list. So at first, I just said while fast.next. So while we're not at the end of the list. Of course this is a little bit dangerous because we might…

Contents