Given a singly linked list and the task is to find middle of 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.

`// C++ program for Recursive approach to find ` `// middle of singly linked list ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Tree Node Structure ` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// Create new Node ` `Node* newLNode(` `int` `data) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->data = data; ` ` ` `temp->next = NULL; ` ` ` `return` `temp; ` `} ` ` ` `// Function for finding midpoint recursively ` `void` `midpoint_util(Node* head, ` `int` `* n, Node** mid) ` `{ ` ` ` ` ` `// If we reached end of linked list ` ` ` `if` `(head == NULL) ` ` ` `{ ` ` ` `*n = (*n) / 2; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `*n = *n + 1; ` ` ` ` ` `midpoint_util(head->next, n, mid); ` ` ` ` ` `// Rolling back, decrement n by one ` ` ` `*n = *n - 1; ` ` ` `if` `(*n == 0) ` ` ` `{ ` ` ` ` ` `// Final answer ` ` ` `*mid = head; ` ` ` `} ` `} ` ` ` `Node* midpoint(Node* head) ` `{ ` ` ` `Node* mid = NULL; ` ` ` `int` `n = 1; ` ` ` `midpoint_util(head, &n, &mid); ` ` ` `return` `mid; ` `} ` ` ` `int` `main() ` `{ ` ` ` `Node* head = newLNode(1); ` ` ` `head->next = newLNode(2); ` ` ` `head->next->next = newLNode(3); ` ` ` `head->next->next->next = newLNode(4); ` ` ` `head->next->next->next->next = newLNode(5); ` ` ` `Node* result = midpoint(head); ` ` ` `cout << result->data << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3

## Recommended Posts:

- Find the middle of a given linked list in C and Java
- Find kth node from Middle towards Head of a Linked List
- Find the common nodes in two singly linked list
- Find smallest and largest elements in singly linked list
- Find minimum and maximum elements in singly Circular Linked List
- Convert singly linked list into circular linked list
- Recursively Reversing a linked list (A simple implementation)
- Delete middle of linked list
- Insert node into the middle of the linked list
- QuickSort on Singly Linked List
- Sum of the nodes of a Singly Linked List
- Make middle node head in a linked list
- Insertion Sort for Singly Linked List
- Product of the nodes of a Singly Linked List
- Circular Singly Linked List | Insertion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.