Write a program to find the middle element of linked list. Suppose linked list is as below:
Head -> 1 -> 2 -> 3 -> 4 -> 5
Output
Middle node of given linked list is: 3
Category: Data Structure And Algorithms | Tags: C# |
Write a program to find the middle element of linked list. Suppose linked list is as below:
Head -> 1 -> 2 -> 3 -> 4 -> 5
Output
Middle node of given linked list is: 3
Answers:
We can solve it using two pointers one is normal pointer increments one and other pointer is called fast pointer which jumps two elements in one iteration. So once fast pointer reaches the end it leaves normal pointer in mid.
Let's run above code.
Output
Middle node of given linked list is: 3