Find Middle Element in Linked List in Python (2 Examples)
Hi! This short tutorial will show you how to determine the middle element in a linked list in the Python programming language.
Here is a quick overview:
Let’s get right into the Python code!
Example 1: Get Middle Element in Linked List Using Two Pointers (Fast & Slow Pointers)
In this first example, we will use two pointers to get the middle element in the linked list.
Therefore, in your Python programming IDE, run the code below:
# build ListNode class class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next # build function def find_middle_element(head): slow_ptr = head fast_ptr = head while fast_ptr is not None and fast_ptr.next is not None: slow_ptr = slow_ptr.next fast_ptr = fast_ptr.next.next return slow_ptr.val # initialize list nodes head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) # apply function middle_element = find_middle_element(head) print(middle_element) # 3
In the example above, we use the concept of two pointers moving at different speeds: a slow pointer slow_ptr
and a fast pointer slow_ptr
. We initialize both pointers to the head of the linked list.
The slow pointer moves one step at a time slow_ptr.next
, while the fast pointer moves two steps at a time fast_ptr.next.next
. By the time the fast pointer reaches the end of the linked list (or the second-to-last element), the slow pointer will be at the middle element.
Hence, once the loop ends, the slow pointer value slow_ptr.val
, which represents the middle element of the linked list, is returned.
You can observe how the find_middle_element()
function returns the middle element 3 for the initialized linked list.
Example 2: Get Middle Element in Linked List By Finding Length & Traversing Again
In this next example, we will return the middle element in the linked list by finding the length of the linked list and traversing it again:
# build ListNode class class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next # build function def find_middle_element(head): current = head length = 0 while current is not None: length += 1 current = current.next middle = length // 2 current = head for _ in range(middle): current = current.next return current.val # intitialize list nodes head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) head.next.next.next = ListNode(4) head.next.next.next.next = ListNode(5) # apply function middle_element = find_middle_element(head) print(middle_element) # 3
Here, we first find the length of the linked list length
by traversing it once. We start from head
and move forward, counting the number of nodes encountered until we reach the end of the linked list.
Then, we calculate the middle position middle
by dividing the length by 2.
After determining the middle position, we traverse the linked list again, starting from the head. We move forward in the linked list, updating the current node current = current.next
, until we reach the middle position.
Once we reach the middle node, we return the current value current.val
.
As seen, compiling the code returns middle_element
as 3, like in Example 1.
Video, Further Resources & Summary
Do you need more explanations on how to find the middle element of a linked list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to find the middle element of a linked list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Find Union of Two Lists in Python (3 Examples)
- How to Use Lists in Python
- Are Dictionaries Faster than Lists in Python?
- What is a List Node in Python? (2 Examples)
- Introduction to Python Programming
This post has shown, using two examples, how to find the middle element of a linked list in Python. Your use case will determine which method to adopt.
I hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter