# C++ program to find middle of the linked list

## Finding the middle  element of a Singly Linked List in C++?

Finding middle of anything means to find the center position it. In this article, we will be learning how to code a C++ program to find middle of the linked list. We will take two pointers for the process one pointer will be moving with twice the speed of second pointer. So when the first pointer will reach the end of the liked list, the second pointer will be at middle of it as it is moving with half the speed of first pointer. In this way center node of the linked list is fetched.

### How to define a linked list?

`class Node  {      int data;      Node *next;  }; `

## Steps to write a C++ program for finding  middle of a linked list

#### These steps are used to find middle the linked list in CPP programming

1. Start the program and initialize the variables.
2. Create a function to create and display list.
3. Now create a function to find middle of the list.
4. In this function initialize two pointers.
5. Traverse one pointer with normal speed.
6. Traverse other pointer with double speed.
7. When the pointer moving with double speed reaches end of the list other pointer will be at middle.
8. Print data at the address of this pointer.

## Algorithm for finding the middle of a linked list

##### Algorithm to find middle of the linked list in CPP programming is given below
• SINGLE_PTR=STNODE
• TWICE_PTR=STNODE
• IF(STNODE!=NULL)
• WHILE(TWICE_PTR!=NULL&&TWICE_PTR->NEXTPTR!=NULL
• TWICE_PTR=TWICE_PTR->NEXTPTR->NEXTPTR
• SINGLE_PTR=SINGLE_PTR->NEXTPTR
• PRINT SINGLE_PTR->NUM

## C++ Program to find middle of linked list

Run

```#include<iostream>
using namespace std;

struct node
{
int num;
node *nextptr;
} *stnode;			//node constructed

void make (int n);
void printMiddle (struct node *stnode);
void display ();

int main ()				//Main method
{
int n, num;

cout << "Enter the number of nodes: "; cin >> n;
make (n);
cout << "\nLinked list data: \n";
display ();
printMiddle (stnode);

return 0;
}

void make (int n)			//function to create linked list.
{
struct node *frntNode, *tmp;
int num, i;

stnode = (struct node *) malloc (sizeof (struct node));
if (stnode == NULL)
{
cout << "Memory can not be allocated";
}
else
{

cout << "Enter the data for node 1: "; cin >> num;
stnode->num = num;
tmp = stnode;

for (i = 2; i <= n; i++)
{
frntNode = (struct node *) malloc (sizeof (struct node));

if (frntNode == NULL)
//If frntnode is null no memory cannot be allotted
{
cout<<" Memory can not be allocated ";
break;
}
else
{
cout<<" Enter the data for node"<<i<<":"; entering="" data="" in="" nodes.="" cin="">>num;
frntNode->num = num;
frntNode->nextptr = NULL;
tmp->nextptr = frntNode;
tmp = tmp->nextptr;
}
}
}
}
void display() // method to display linked list
{
struct node *tmp;
if(stnode == NULL)
{
cout<<" List is empty ";
}
else
{
tmp = stnode;
while(tmp != NULL)
{
cout<num<<" \t "; tmp = tmp->nextptr;
}
}
}
void printMiddle(struct node *stnode)//function to print middle node of the list.
{
struct node *single_ptr = stnode;
struct node *twice_ptr = stnode;

if (stnode!=NULL)
{
while (twice_ptr != NULL && twice_ptr->nextptr != NULL)
{
twice_ptr = twice_ptr->nextptr->nextptr; //moving with twice speed.
single_ptr = single_ptr->nextptr;
}
cout<<" The middle element is "<num;
}
}

</i<<":";>```
```Output:
Enter the number of nodes: 5
Enter the data for node 1: 5
Enter the data for node 2: 9
Enter the data for node 3: 8
Enter the data for node 4: 4
Enter the data for node 5: 6

Linked List	5	9	8	4	6
The middle element is 8```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Linked List in Data Structure
C | C++ | Java
• Singly Linked List in –
C | C++ | Java
• Insertion in singly Linked List –
C | C++ | Java
• Insertion at beginning in singly Linked List  –
C | C++Java
• Insertion at nth position in singly Linked List  –
C | C++Java
• Insertion at end in singly Linked List  –
C | C++Java
• Deletion in singly Linked List  –
C | C++Java
• Deletion from beginning in singly linked list :
C | C++ | Java
• Deletion from nth position in singly linked list :
C | C++ | Java
• Deletion from end in singly linked list :
C | C++ | Java
• Linked List Insertion and Deletion –
C | C++Java
• Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++Java
C | C++Java
• Print reverse of a linked list without actually reversing –
C |C++Java
• Print reverse of a linked list without actually reversing –
C |C++Java
• Insertion in the middle Singly Linked List –
C | C++Java
• Insertion in a Sorted Linked List –
C | C++Java
• Delete alternate nodes of a Linked List –
C | C++Java
• Find middle of the linked list –
C | C++Java
• Reverse a linked list in groups of given size –
C | C++Java
• Find kth node from end of the linked list –
C | C++Java
• Append the last n nodes of a linked list to the beginning of the list –
C | C++Java
• Check whether linked list is palindrome or not –
C | C++Java
• Fold a Linked List –
C | C++Java
• Insert at given Position –
C | C++Java
• Deletion at given Position –
C | C++Java

• Introduction to Linked List in Data Structure
• Linked List in – C | C++ | Java
• Singly Linked List in – C | C++ | Java
• Insertion in singly Linked List – C | C++ | Java
• Insertion at beginning in singly Linked List  – C | C++Java
• Insertion at nth position in singly Linked List  – C | C++Java
• Insertion at end in singly Linked List  – C | C++Java
• Deletion in singly Linked List  – C | C++Java
• Deletion from beginning in singly linked list : C | C++ | Java
• Deletion from nth position in singly linked list : C | C++ | Java
• Deletion from end in singly linked list : C | C++ | Java
• Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
• Linked List Insertion and Deletion – C | C++Java
• Reverse a linked list by changing links between nodes – C | C++Java
• Linked List insertion in the middle – C | C++Java
• Print reverse of a linked list without actually reversing – C |C++Java
• Search an element in a linked list – C | C++Java
• Insertion in a Sorted Linked List – C | C++Java
• Delete alternate nodes of a Linked List – C | C++Java
• Find middle of the linked list – C | C++Java
• Reverse a linked list in groups of given size – C | C++Java
• Find kth node from end of the linked list – C | C++Java
• Append the last n nodes of a linked list to the beginning of the list – C | C++Java
• Check whether linked list is palindrome or not – C | C++Java
• Fold a Linked List – C | C++Java
• Insert at a given position – C | C++Java
• Delete at a given position – C | C++Java