Open in App
Not now

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

• Difficulty Level : Easy
• Last Updated : 02 Mar, 2023

Given a singly linked list, find the middle of the linked list. Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the 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.

## Python3

 `# Python program for the above approach` `class` `Node:` `    ``def` `__init__(``self``, data):` `        ``self``.data ``=` `data` `        ``self``.``next` `=` `None`     `class` `NodeOperation:` `    ``# Function to add a new node` `    ``def` `pushNode(``self``, head_ref, data_val):`   `        ``# Allocate node and put in the data` `        ``new_node ``=` `Node(data_val)`   `        ``# Link the old list of the new node` `        ``new_node.``next` `=` `head_ref`   `        ``# move the head to point to the new node` `        ``head_ref ``=` `new_node` `        ``return` `head_ref`   `    ``# A utility function to print a given linked list` `    ``def` `printNode(``self``, head):` `        ``while` `(head !``=` `None``):` `            ``print``(``'%d->'` `%` `head.data, end``=``"")` `            ``head ``=` `head.``next` `        ``print``(``"NULL"``)`   `    ``''' Utility Function to find length of linked list '''`   `    ``def` `getLen(``self``, head):` `        ``temp ``=` `head` `        ``len` `=` `0`   `        ``while` `(temp !``=` `None``):` `            ``len` `+``=` `1` `            ``temp ``=` `temp.``next`   `        ``return` `len`   `    ``def` `printMiddle(``self``, head):` `        ``if` `head !``=` `None``:` `            ``# find length` `            ``len` `=` `self``.getLen(head)` `            ``temp ``=` `head`   `            ``# traverse till we reached half of length` `            ``midIdx ``=` `len` `/``/` `2` `            ``while` `midIdx !``=` `0``:` `                ``temp ``=` `temp.``next` `                ``midIdx ``-``=` `1`   `            ``# temp will be storing middle element` `            ``print``(``'The middle element is: '``, temp.data)`     `# Driver Code` `head ``=` `None` `temp ``=` `NodeOperation()` `head ``=` `temp.pushNode(head, ``5``)` `head ``=` `temp.pushNode(head, ``4``)` `head ``=` `temp.pushNode(head, ``3``)` `head ``=` `temp.pushNode(head, ``2``)` `head ``=` `temp.pushNode(head, ``1``)` `temp.printNode(head)` `temp.printMiddle(head)`   `# This code is contributed by Yash Agarwal`

Output

```1->2->3->4->5->NULL
The middle element is:  3```

Time Complexity: O(n) where n is no of nodes in linked list
Auxiliary Space: O(1)

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

Implementation:

## Python3

 `# 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): ` `        ``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`   `    ``# 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` `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)`   `# Driver code` `list1 ``=` `LinkedList()` `list1.push(``5``)` `list1.push(``4``)` `list1.push(``2``)` `list1.push(``3``)` `list1.push(``1``)` `list1.printMiddle()`

Output

`The middle element is:  2`

Time Complexity: O(N) where N is the number of nodes in Linked List.

Auxiliary Space: O(1)

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.

Implementation:

## Python3

 `# Python 3 program to find the middle of a` `# given linked list`   `class` `Node:` `    ``def` `__init__(``self``, value):` `        ``self``.data ``=` `value` `        ``self``.``next` `=` `None` `    `  `class` `LinkedList:`   `    ``def` `__init__(``self``):` `        ``self``.head ``=` `None`   `    ``# create Node and make linked list` `    ``def` `push(``self``, new_data):` `        ``new_node ``=` `Node(new_data)` `        ``new_node.``next` `=` `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 ``=` `temp.``next` `            ``self``.head ``=` `self``.head.``next`   `            ``# increment count in each iteration` `            ``count ``+``=` `1` `        `  `        ``print``(temp.data)    ` `        `  `# Driver code` `llist ``=` `LinkedList()` `llist.push(``1``)` `llist.push(``20``)` `llist.push(``100``)` `llist.push(``15``)` `llist.push(``35``)` `llist.printMiddle()` `# code has been contributed by - Yogesh Joshi`

Output

`100`

Complexity Analysis:

• Time complexity: O(N) where N is the size of the given linked list
• Auxiliary Space: O(1) because it is using constant space

My Personal Notes arrow_drop_up
Related Articles