C program to find the middle node of a linked list

Question :- C program to find the middle node of a linked list

The simple approach to reach up to middle node we have to know how many nodes are there in linked list and then traverse up to half of that node count. But this solution is not efficient.

Algorithm

 

But there is another way which is very efficient. Traverse linked list using two pointers named as fast and slow. Move slow pointer by one node and fast pointer by two node in single iteration. When the fast pointer reaches end slow pointer will reach middle of the linked list.

C program to find the middle node of a linked list

 

Output

C program to find the middle node of a linked list

Suggested Reading

  1. Write a program to reverse a linked list using stack in c++
  2. Write a c program to implement a queue using array and linked list
  3. Write a c program to implement a stack using an array and linked list