# C Program to find middle element in the linked list

## Finding middle element of the linked list using C

C Program to find middle element of the linked list. To learn how to find middle element of singly linked list in one pass you may need to adjust two pointers, one increment at each node while other pointer increments after two nodes at a time by having such arrangements when the first pointer reaches end then second pointer will point to middle element of the linked list.If there are even number of nodes then there will be possibility of two middle nodes in which we need to print second middle element of the node.
For Example:- If the given linked list is,
Input:-1->2->3->4->5->6, then
Output:- 4 ## Implementation of finding middle element of the linked list:-

• First take two pointers first node and second node
• Initially first node increment by one pass and second node increment after two nodes passes.
• Make the link of first node to point to link of q and free q.
• Then move first node to its end.
• After that move second node to middle of the list
• Return second node element. ## Constructing node Structure in the following box:-

`struct node{int data;struct node *next;};`

## C Program to find middle element of the linked list:-

Run
```#include<stdio.h>
#include<stdlib.h>

{
int data;
struct Node *next;
};

void display (struct Node *node)
{

//as linked list will end when Node is Null
while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}

//Function found the middle element in the linked list
{

{
//the only logic is to traverse the linked list with two pointers
//one at normal speed and other twice the speed of first
/*when the fast pointer reaches to the end, slow pointer will be in
the middle of the linted list */
while (second_ptr != NULL && second_ptr->next != NULL)
{
second_ptr = second_ptr->next->next;
first_ptr = first_ptr->next;
}
printf ("The middle element in the linked list is : %d",
first_ptr->data);
}
}

int main ()
{
//creating 4 pointers of type struct Node
//So these can point to address of struct type variable
struct Node *node2 = NULL;
struct Node *node3 = NULL;
struct Node *node4 = NULL;
struct Node *node5 = NULL;

// allocate 3 nodes in the heap
head = (struct Node *) malloc (sizeof (struct Node));
node2 = (struct Node *) malloc (sizeof (struct Node));
node3 = (struct Node *) malloc (sizeof (struct Node));
node4 = (struct Node *) malloc (sizeof (struct Node));
node5 = (struct Node *) malloc (sizeof (struct Node));

node2->data = 15;
node2->next = node3;

node3->data = 31;
node3->next = node4;

node4->data = 44;
node4->next = node5;

node5->data = 92;
node5->next = NULL;

return 0;
}
```

#### Output

```Linked List: 80 15 31 44 92
The middle element in the linked list is : 31
```

### 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
• 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

• 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