# Print the Middle of a given linked list

This problem is similar to the finding the middle element of linked list. we can easily solve this problem using two pointers. Let see an example.

In case linked list are contain even number of linked list nodes in this case there will be two possibilities of middle element. See this example.

``1->2->3->4->5->6->7->8->NULL``

In this situation [4,5] both are middle node. Here given code implementation process.

``````//C Program to print middle node of given linked list
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data;
struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
//insert Node element
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
node->data=value;
}
}
//print middle node
printf("\nLess than 3 nodes");
}else{
while(temp!=NULL &&temp->next!=NULL && temp->next->next){
mid_element=mid_element->next;
temp=temp->next->next;
}
printf("\nMiddle Element : %d\n",mid_element->data);
}

}
//Display element of Node
void display(struct Node*temp){

if(temp==NULL){
}else{
while(temp!=NULL){
printf("%d  ",temp->data);
temp=temp->next;
}
}

}
int main(){
//create node pointer
//display all node

return 0;
}``````

#### Output

``````Linked List Node :1  2  3  4  5  6  7
Middle Element : 4
``````
``````//C++ Program to print middle node of linked list

#include<iostream>
using namespace std;

//create structure
struct Node{
int data;
struct Node*next;
};
public:
void insert(int);
void display();
void print_mid();
};

}

//Insert Node at beginning of list
//Create dynamic node
struct Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow"<<endl;
}else{
node->data=value;
}
}
//Display element of Node

}
else{
while(temp!=NULL){
cout<<"  "<<temp->data;
temp=temp->next;
}

}
}
//Print middle nodes
cout<<"Less than 2 nodes of this Linked List"<<endl;
}else{
while(temp!=NULL&&temp->next&&temp->next->next){
middle=middle->next;
temp=temp->next->next;
}
cout<<"\nMiddle Element : "<<middle->data<<endl;
}

}
int main(){
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);
//display all node
obj.display();
obj.print_mid();
return 0;
}``````

#### Output

``````Linked List element :  1  2  3  4  5  6  7
Middle Element : 4``````
``````//Java program to print middle of linked list
class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}

}
public void insert(int value) {
Node newNode = new Node(value);
}
//print middle node of linked list
public void printMid() {
//when linked list are no elements
//when linked list are less than  of 3 nodes
System.out.println("Less then 3 node of linked list");
} else {
while (temp != null && temp.next != null && temp.next.next != null) {

midNode = midNode.next;
temp = temp.next.next;
}

System.out.println("\nMiddle node : " + midNode.data);

}
}
public void display() {

while (temp != null) {
System.out.print(" " + temp.data);
temp = temp.next;
}
} else {
}
}
public static void main(String arg[]) {
info.insert(7);
info.insert(6);
info.insert(5);
info.insert(4);
info.insert(3);
info.insert(2);
info.insert(1);

info.display();
info.printMid();

}
}``````

#### Output

``````Linked List Elements : 1 2 3 4 5 6 7
Middle node : 7``````
``````#Python print middle node of linked list

class Node:
def __init__(self,data):
self.data=data
self.next=None

#create class element
def __init__(self):
#Assign default value

#insert new node to linked list
def insert(self,data):
node=Node(data)

def display(self):
while(temp!=None):
print(temp.data,end=" ")
temp=temp.next

#print middle node
def printMiddle(self):
print("Empty element list\n")
print("Less then 3 node of element list")
else:
while(temp!=None and temp.next!=None and temp.next.next):
temp=temp.next.next
middle=middle.next

print("\nMiddle Node : ",middle.data)

def main():
#Create Object of class element

#element list insertion
element.insert(7)
element.insert(6)
element.insert(5)
element.insert(4)
element.insert(3)
element.insert(2)
element.insert(1)
element.display()
element.printMiddle()

if __name__=="__main__":
main()    ``````

#### Output

``````1 2 3 4 5 6 7
Middle Node :  4``````
``````//C# print middle node of linked list
using System;
public class Node{
public  int data;
public  Node next;
}
class Program
{
public Program(){

}
//insert node at beginning
public void insert(int data){
//make a new node
Node element=new Node();
element.data=data;

}
//Find and print middle node of linked list
public void printMid(){

}else{
while(temp!=null && temp.next!=null &&temp.next.next!=null){
midElement=midElement.next;
temp=temp.next.next;
}
Console.WriteLine("\nMiddle Node : {0}",midElement.data);
}
}
//display node elements
public void display(){
Console.Write("Empty List");
}else{
Console.Write("\nNode Elements :");
while(temp!=null){
Console.Write("  {0}",temp.data);
temp=temp.next;
}

}
}
static void Main()
{
Program obj=new Program();
obj.insert(7);
obj.insert(6);
obj.insert(5);
obj.insert(4);
obj.insert(3);
obj.insert(2);
obj.insert(1);

obj.display();
obj.printMid(); //print mid node

}
}``````

#### Output

``````Node Elements :  1  2  3  4  5  6  7
Middle Node : 4``````
``````<?php
//Php program
//print middle node of linked list
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);
{
}else{
//find last node of linked list
while(\$temp->next!=NULL){
\$temp=\$temp->next;
}
\$temp->next=\$newNode;
}
}
//Remove middle nodes
function print_mid(){
echo "<br>Less than 2 nodes of this Linked List";
}else{
\$middle=NULL;
//find middle node
while(\$temp!=NULL&&\$temp->next&&\$temp->next->next){

\$middle=\$middle->next;

\$temp=\$temp->next->next;
}
echo "<br> Middle Node : ".\$middle->data;
}

}
//Display all inserted node in linked list
function display()
{
{
}
else{
while(\$temp!=NULL){
//display node value
echo "&nbsp;&nbsp;".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}
}
function main(){
//Make a object of LinkedList class
/*
*Insert following nodes in linked list
*/
\$obj->insert(1);
\$obj->insert(2);
\$obj->insert(3);
\$obj->insert(4);
\$obj->insert(5);
\$obj->insert(6);
\$obj->insert(7);

//Display all node
\$obj->display();
\$obj->print_mid();

}
main();
?>``````

#### Output

``````Linked List :  1  2  3  4  5  6  7
Middle Node : 4``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.