![]() |
VOOZH | about |
Given a singly linked list and the task is to find the middle of the linked list.
Examples:
Input : 1->2->3->4->5 Output : 3 Input : 1->2->3->4->5->6 Output : 4
We have already discussed Iterative Solution. In this post iterative solution is discussed. Count total number of nodes in the list in recursive manner and do half of this, suppose this value is n. Then rolling back through recursion decrement n by one for each call. Return the node where n is zero.
Implementation:
3
Time Complexity: O(N) where N is the number of nodes in the Linked List.
Auxiliary Space: O(N), due to recursion call stack