Given the head of singly linked list, find middle node of the linked list.
If the number of nodes is odd, return the middle node.
If the number of nodes is even, there are two middle nodes, so return the second middle node.
Example:
Input:
Output: 3 Explanation: There are 5 nodes in the linked list and there is one middle node whose value is 3.
Input:
Output: 40 Explanation: There are 6 nodes in the linked list, so we have two middle nodes: 30 and 40, but we will return the second middle node which is 40.
[Naive Approach] Two Passes - O(n) Time and O(1) Space
The idea is to traverse the entire linked list once to count the total number of nodes. After determining the total count, traverse the list again and stop at the (count/2)th node to return its value. This method requires two passes through the linked list to find the middle element.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){this->data=x;this->next=nullptr;}};intgetLength(Node*head){intlength=0;// Count nodes in linked listwhile(head){length++;head=head->next;}returnlength;}intgetMiddle(Node*head){// finding length of the linked listintlength=getLength(head);// traverse till we reached half of lengthintmidIndex=length/2;while(midIndex--){head=head->next;}returnhead->data;}intmain(){Node*head=newNode(10);head->next=newNode(20);head->next->next=newNode(30);head->next->next->next=newNode(40);head->next->next->next->next=newNode(50);head->next->next->next->next->next=newNode(60);cout<<getMiddle(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};intgetLength(structNode*head){intlength=0;// find length of linked listwhile(head){length++;head=head->next;}returnlength;}intgetMiddle(structNode*head){// finding length of the linked listintlength=getLength(head);// traverse till we reach half of the lengthintmidIndex=length/2;while(midIndex--){head=head->next;}returnhead->data;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}intmain(){structNode*head=createNode(10);head->next=createNode(20);head->next->next=createNode(30);head->next->next->next=createNode(40);head->next->next->next->next=createNode(50);head->next->next->next->next->next=createNode(60);printf("%d\n",getMiddle(head));return0;}
Java
classNode{intdata;Nodenext;Node(intx){this.data=x;this.next=null;}}classGfG{staticintgetLength(Nodehead){intlength=0;// Count nodes in linked listwhile(head!=null){length++;head=head.next;}returnlength;}staticintgetMiddle(Nodehead){intlength=getLength(head);// traverse till we reached half of lengthintmidIndex=length/2;while(midIndex>0){head=head.next;midIndex--;}returnhead.data;}publicstaticvoidmain(String[]args){Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);head.next.next.next.next=newNode(50);head.next.next.next.next.next=newNode(60);System.out.println(getMiddle(head));}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefgetLength(head):length=0# Count nodes in linked listwhilehead:length+=1head=head.nextreturnlengthdefgetMiddle(head):length=getLength(head)# traverse till we reach half of the lengthmidIndex=length//2whilemidIndex:head=head.nextmidIndex-=1returnhead.dataif__name__=="__main__":head=Node(10)head.next=Node(20)head.next.next=Node(30)head.next.next.next=Node(40)head.next.next.next.next=Node(50)head.next.next.next.next.next=Node(60)print(getMiddle(head))
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){this.data=x;this.next=null;}}classGfG{staticintgetLength(Nodehead){intlength=0;// Count nodes in linked listwhile(head!=null){length++;head=head.next;}returnlength;}staticintgetMiddle(Nodehead){intlength=getLength(head);// traverse till we reach half of the lengthintmidIndex=length/2;while(midIndex-->0){head=head.next;}returnhead.data;}staticvoidMain(){Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);head.next.next.next.next=newNode(50);head.next.next.next.next.next=newNode(60);Console.WriteLine(getMiddle(head));}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functiongetLength(head){letlength=0;// Count nodes in linked listwhile(head){length++;head=head.next;}returnlength;}functiongetMiddle(head){constlength=getLength(head);// traverse till we reach half of the lengthletmidIndex=Math.floor(length/2);while(midIndex-->0){head=head.next;}returnhead.data;}// Driver Codeconsthead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);head.next.next.next.next=newNode(50);head.next.next.next.next.next=newNode(60);console.log(getMiddle(head));
Output
40
[Expected Approach] Hare and Tortoise Algorithm - O(n) Time and O(1) Space
We can use the Tortoise and Hare algorithm to find the middle of the linked list.
Initialize both slow and fast pointers at the head.
Move slow by one step and fast by two steps each iteration.
When fast reaches the end (or null), slow will be at the middle.
For even nodes, slow automatically ends at the second middle.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){this->data=x;this->next=nullptr;}};intgetMiddle(Node*head){Node*slowptr=head;Node*fastptr=head;while(fastptr!=NULL&&fastptr->next!=NULL){// move the fast pointer by two nodesfastptr=fastptr->next->next;// move the slow pointer by one nodeslowptr=slowptr->next;}returnslowptr->data;}intmain(){Node*head=newNode(10);head->next=newNode(20);head->next->next=newNode(30);head->next->next->next=newNode(40);head->next->next->next->next=newNode(50);head->next->next->next->next->next=newNode(60);cout<<getMiddle(head)<<endl;return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};intgetMiddle(structNode*head){structNode*slowptr=head;structNode*fastptr=head;// traverse the linked listwhile(fastptr!=NULL&&fastptr->next!=NULL){// move the fast pointer by two nodesfastptr=fastptr->next->next;// move the slow pointer by one nodeslowptr=slowptr->next;}returnslowptr->data;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}intmain(){structNode*head=createNode(10);head->next=createNode(20);head->next->next=createNode(30);head->next->next->next=createNode(40);head->next->next->next->next=createNode(50);head->next->next->next->next->next=createNode(60);printf("%d\n",getMiddle(head));return0;}
Java
classNode{intdata;Nodenext;Node(intx){this.data=x;this.next=null;}}classGfG{staticintgetMiddle(Nodehead){Nodeslowptr=head;Nodefastptr=head;while(fastptr!=null&&fastptr.next!=null){// move the fast pointer by two nodesfastptr=fastptr.next.next;// move the slow pointer by one nodeslowptr=slowptr.next;}returnslowptr.data;}publicstaticvoidmain(String[]args){Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);head.next.next.next.next=newNode(50);head.next.next.next.next.next=newNode(60);System.out.println(getMiddle(head));}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefgetMiddle(head):slowptr=headfastptr=headwhilefastptrisnotNoneandfastptr.nextisnotNone:# move the fast pointer by two nodesfastptr=fastptr.next.next# move the slow pointer by one nodeslowptr=slowptr.nextreturnslowptr.dataif__name__=="__main__":head=Node(10)head.next=Node(20)head.next.next=Node(30)head.next.next.next=Node(40)head.next.next.next.next=Node(50)head.next.next.next.next.next=Node(60)print(getMiddle(head))
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){this.data=x;this.next=null;}}classGfG{staticintgetMiddle(Nodehead){Nodeslowptr=head;Nodefastptr=head;while(fastptr!=null&&fastptr.next!=null){// move the fast pointer by two nodesfastptr=fastptr.next.next;// move the slow pointer by one nodeslowptr=slowptr.next;}returnslowptr.data;}staticvoidMain(){Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);head.next.next.next.next=newNode(50);head.next.next.next.next.next=newNode(60);Console.WriteLine(getMiddle(head));}}
JavaScript
classNode{constructor(new_data){this.data=new_data;this.next=null;}}functiongetMiddle(head){letslowptr=head;letfastptr=head;while(fastptr!==null&&fastptr.next!==null){// move the fast pointer by two nodesfastptr=fastptr.next.next;// Move the slow pointer by one nodeslowptr=slowptr.next;}returnslowptr.data;}// Driver Codeconsthead=newNode(10);head.next=newNode(20);head.next.next=newNode(30);head.next.next.next=newNode(40);head.next.next.next.next=newNode(50);head.next.next.next.next.next=newNode(60);console.log(getMiddle(head));