Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python program to find middle of a linked list using one traversal

  • Difficulty Level : Medium
  • Last Updated : 30 Dec, 2020

Given a singly linked list, find middle of the linked list. Given a singly linked list, find middle of the linked list. For example, if given linked list is 1->2->3->4->5 then output should be 3.

Method 1:
Traverse the whole linked list and count the no. of nodes. Now traverse the list again till count/2 and return the node at count/2.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Method 2:
Traverse linked list using two pointers. Move one pointer by one and other pointer by two. When the fast pointer reaches end slow pointer will reach middle of the linked list.

# Python 3 program to find the middle of a  
# given linked list 
# Node class 
class Node: 
    # Function to initialise the node object 
    def __init__(self, data): = data = None 
class LinkedList:
    def __init__(self):
        self.head = None
    def push(self, new_data):
        new_node = Node(new_data) = self.head
        self.head = new_node
    # Function to get the middle of 
    # the linked list
    def printMiddle(self):
        slow_ptr = self.head
        fast_ptr = self.head
        if self.head is not None:
            while (fast_ptr is not None and is not None):
                fast_ptr =
                slow_ptr =
            print("The middle element is: ",
# Driver code
list1 = LinkedList()
The middle element is:  2

Method 3:
Initialized the temp variable as head
Initialized count to Zero
Take loop till head will become Null(i.e end of the list) and increment the temp node when count is odd only, in this way temp will traverse till mid element and head will traverse all linked list. Print the data of temp.

# Python 3 program to find the middle of a  
# given linked list 
class Node:
    def __init__(self, value): = value = None
class LinkedList:
    def __init__(self):
        self.head = None
    # create Node and and make linked list
    def push(self, new_data):
        new_node = Node(new_data) = self.head
        self.head = new_node
    def printMiddle(self):
        temp = self.head 
        count = 0
        while self.head:
            # only update when count is odd
            if (count & 1): 
                temp =
            self.head =
            # increment count in each iteration 
            count += 1 
# Driver code
llist = LinkedList() 
# code has been contributed by - Yogesh Joshi

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!