Find middle of singly linked list Recursively
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.
C++
// C++ program for Recursive approach to find// middle of singly linked list#include <iostream>using namespace std;// Tree Node Structurestruct Node{ int data; struct Node* next;};// Create new NodeNode* newLNode(int data){ Node* temp = new Node; temp->data = data; temp->next = NULL; return temp;}// Function for finding midpoint recursivelyvoid 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;, ∣); 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;} |
Java
// Java program for Recursive approach to find// middle of singly linked listclass GFG{// Tree Node Structurestatic class Node{ int data; Node next;};// Create new Nodestatic Node newLNode(int data){ Node temp = new Node(); temp.data = data; temp.next = null; return temp;}static int n;static Node mid;// Function for finding midpoint recursivelystatic void midpoint_util(Node head ){ // If we reached end of linked list if (head == null) { n = (n) / 2; return; } n = n + 1; midpoint_util(head.next); // Rolling back, decrement n by one n = n - 1; if (n == 0) { // Final answer mid = head; }}static Node midpoint(Node head){ mid = null; n = 1; midpoint_util(head); return mid;}// Driver codepublic static void main(String args[]){ 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); System.out.print( result.data );}}// This code is contributed by Arnab Kundu |
Python3
# Python3 program for Recursive approach# to find middle of singly linked list# Node classclass Node: # Function to initialise the node object def __init__(self, data): self.data = data self.next = None # Create new Nodedef newLNode(data): temp = Node(data) temp.data = data temp.next = None return tempmid = Nonen = 0# Function for finding midpoint recursivelydef midpoint_util(head ): global n global mid # If we reached end of linked list if (head == None): n = int((n) / 2) return n = n + 1 midpoint_util(head.next) # Rolling back, decrement n by one n = n - 1 if (n == 0): # Final answer mid = headdef midpoint(head): global n global mid mid = None n = 1 midpoint_util(head) return mid# Driver Codeif __name__=='__main__': 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) result = midpoint(head) print( result.data ) # This code is contributed by Arnab Kundu |
C#
// C# program for Recursive approach to find// middle of singly linked listusing System;class GFG{// Tree Node Structurepublic class Node{ public int data; public Node next;};// Create new Nodestatic Node newLNode(int data){ Node temp = new Node(); temp.data = data; temp.next = null; return temp;}static int n;static Node mid;// Function for finding midpoint recursivelystatic void midpoint_util(Node head ){ // If we reached end of linked list if (head == null) { n = (n) / 2; return; } n = n + 1; midpoint_util(head.next); // Rolling back, decrement n by one n = n - 1; if (n == 0) { // Final answer mid = head; }}static Node midpoint(Node head){ mid = null; n = 1; midpoint_util(head); return mid;}// Driver codepublic static void 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); Console.WriteLine( result.data );}}// This code is contributed by Rajput-Ji |
Javascript
<script>// JavaScript program for Recursive approach to find// middle of singly linked list// Tree Node Structureclass Node{ constructor() { this.data = 0; this.next = null; }};// Create new Nodefunction newLNode(data){ var temp = new Node(); temp.data = data; temp.next = null; return temp;}var n = 0;var mid = null;;// Function for finding midpoint recursivelyfunction midpoint_util(head){ // If we reached end of linked list if (head == null) { n = (n) / 2; return; } n = n + 1; midpoint_util(head.next); // Rolling back, decrement n by one n = n - 1; if (n == 0) { // Final answer mid = head; }}function midpoint(head){ mid = null; n = 1; midpoint_util(head); return mid;}// Driver codevar 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);var result = midpoint(head);document.write( result.data );</script> |
Output:
3



