Last Updated on July 24, 2023 by Mayank Dham

Linked lists, with their dynamic and flexible nature, are fundamental data structures used in computer science and software development. They offer an elegant way to organize and manage data, and finding the middle element of a linked list is a crucial operation that plays a significant role in various algorithms and applications. Whether you are a seasoned Python developer or a curious enthusiast, this article will serve as your compass in navigating the intricacies of linked lists and unveil the strategies to efficiently locate the middle element.

In this comprehensive guide, we will explore various methods and algorithms to identify the middle element in a linked list using Python. Starting with the basics of linked lists and their implementation, we will journey through iterative and recursive approaches, each providing unique insights into tackling this essential task. Additionally, we’ll discuss the time and space complexities of each method to understand their efficiency and applicability for different scenarios.

Python program for middle element of linked list

### Problem Statement on how to find middle element of linked list in python

In this problem, we are given a singly linked list. We have to find the middle of the linked list in a single traversal.

### Problem Statement Understanding for Python program for middle element of linked list

Usually, whenever we find the middle of a linked list, 2 traversals are needed.

- 1
^{st}traversal to find the length of the list. - 2
^{nd}traversal to reach the (length/2)^{th}node of the list which is actually the middle node of the linked list.

But here we have to find the middle of the linked list in one traversal.

Suppose the given list is 5 → 10 → 15 → 4 → 8. The middle of the list will be 15.

**Input :**

**Output :** 15

**Explanation:** The middle of the given list is 15.

As stated in the problem statement, we have to find the middle of the list in a single traversal. How can we tackle this?

- What if we start by taking two pointers, say slow and fast, and make both of them point to the head initially.
- Now, what will happen if we make the slow jump one place and the fast jump two places (fast moving with twice the speed of slow).
- If we notice carefully, by doing the above step, when the fast will reach the end of the list, the slow will be pointing to the middle of the list.
- With the help of this technique, we can reach the middle node of a linked list in a single pass and hence our objective of finding the middle of linked list in one traversal will be achieved.

The reason why our slow pointer will be pointing to the middle of the linked list when our fast pointer is at end is that, as our slow pointer is travelling with half the speed of fast pointer so when the fast pointer will reach the end of the linked list, till that time slow pointer should have only travelled half the distance as travelled by fast pointer and hence it will be at the middle of the linked list.

Do you know what the above-explained method is called?

This above approach is called the slow and fast pointer method. The slow and fast pointer method has been explained in detail in the approach section and in the dry run.

Let us have a glance at the approach.

### Approach for Python program for middle element of linked list

Let us see what the slow and fast pointer technique is:

Initially, both the slow and fast pointer will point to the head of the linked list. Then, we will move both of the pointers slow and fast until fast reaches the end. The fast pointer will jump two places, whereas the slow pointer will one place.

When the fast pointer will reach the end of the linked list, the slow pointer will be pointing to the middle of the linked list.

### Algorithm on how to find middle element of linked list in python

- Create two pointers slow and fast.
- Initially both slow and fast will be pointing to the head of the list.
- Now, make the slow pointer jump one place and the fast pointer jump two places until fast reaches the end of the list.
- When the fast pointer reaches the end of the list, the slow pointer will be pointing to the middle of the list.
- In the end, return the data of the slow pointer, as the slow will be pointing to the middle of the list.

### Dry Run for Python program for middle element of linked list

### Code Implementation for how to find middle element of linked list in python

class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node def printMiddle(self): slow_ptr = self.head fast_ptr = self.head if self.head is not None: while (fast_ptr is not None and fast_ptr.next is not None): fast_ptr = fast_ptr.next.next slow_ptr = slow_ptr.next print("The middle element is: ", slow_ptr.data) list1 = LinkedList() list1.push(8) list1.push(4) list1.push(15) list1.push(10) list1.push(5) list1.printMiddle()

#### Output

The middle element is: 15

**Time Complexity for Python program for middle element of linked list:** O(n), as list traversal is needed.

**Space Complexity for how to find middle element of linked list in python:** The time complexity to find the middle of the linked list python is O(1).

**Conclusion**

In conclusion, finding the middle of the linked list python is a fundamental operation with various efficient approaches at our disposal. Throughout this article, we explored different methods to achieve this task, each offering its unique advantages and insights. We started by understanding the basics of linked lists and their implementation, paving the way for a deeper understanding of the middle element search.

## FAQ on How to Find Middle Element of Linked List in Python

**Q: Can I use the iterative method to find the middle element in a singly or doubly linked list?**

A: Yes, the iterative approach is applicable to both singly and doubly linked lists. In a singly linked list, the slow and fast pointers are sufficient to find the middle element, whereas in a doubly linked list, you can adjust the movement of the fast pointer accordingly.

**Q: How does the iterative method work in finding the middle element?**

A: The iterative approach employs two pointers – slow and fast. The slow pointer advances one step at a time, while the fast pointer moves two steps at a time. By the time the fast pointer reaches the end of the list, the slow pointer will be at the middle element.

**Q: What is the time complexity of finding the middle element using the iterative method?**

A: The time complexity is O(n), where n is the number of nodes in the linked list. Since both the slow and fast pointers traverse the list once, the time taken is directly proportional to the list’s size.

**Q: Can I find the middle element in a circular linked list using the iterative method?**

A: Yes, the iterative approach works for circular linked lists as well. The fast pointer will eventually "catch up" with the slow pointer, indicating that they have both traversed half the circular list.

**Q: Is it possible to find the middle element of a linked list in a single pass?**

A: Yes, the iterative method does exactly that – finding the middle element in a single pass over the linked list. It is an efficient and optimal way to locate the middle element without requiring multiple iterations.

**Q: Can I use the recursive method to find the middle element in a singly circular linked list?**

A: Yes, the recursive method is applicable to both singly linked lists and singly circular linked lists. However, you should be cautious about the increased space complexity due to the recursive calls.