Home » Data Structure & Algorithms » Middle Element of LinkedList

Middle Element of LinkedList


Question :
WAP to find the middle element of Linked List in O(n) time complexity.

Solution :
The problem is solved in iterative  way.

Algorithm :
Step 1 : If head is null, then return -1.
Step 2 : Take two pointer fast and slow.Fast pointer moves by two position and slow pointer moves by one position.
Step 3 : When the fast pointer reaches end,the  slow pointer will reach middle of the linked list.

Program :

OutPut :


Leave a comment

Your email address will not be published. Required fields are marked *

one × three =