Write a program in C to print the middle most node of a given linked list
Find the middle of a given linked listGiven a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. Show C Program For Finding The Middle Element Of A Given Linked ListGiven a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Method 1: Method 2: Below image shows how printMiddle function works in the code : C
Output: 5->NULL The middle element is [5] 4->5->NULL The middle element is [5] 3->4->5->NULL The middle element is [4] 2->3->4->5->NULL The middle element is [4] 1->2->3->4->5->NULL The middle element is [3]Method 3: C
Output: 5->NULL The middle element is [5] 4->5->NULL The middle element is [5] 3->4->5->NULL The middle element is [4] 2->3->4->5->NULL The middle element is [4] 1->2->3->4->5->NULL The middle element is [3]Please refer complete article on Find the middle of a given linked list for more details!
Article Tags :
C Language C Programs Linked List
Adobe Amazon Flipkart GE Hike Linked Lists MAQ Software Microsoft Morgan Stanley Nagarro Payu Qualcomm Samsung Veritas VMWare Wipro Zoho Practice Tags :
VMWare Zoho Flipkart Morgan Stanley Amazon Microsoft Samsung Hike Payu MAQ Software Adobe Wipro Qualcomm Nagarro GE Veritas Linked List
Read Full Article
Find middle node of a linked list using slow and fast pointer.Algorithm to print middle node of linked list Let "head" be the head pointer of given linked list.
In this program, we will use a user defined function "printMiddleNode" which takes head node of a linked list as input and print middle node by implementing above mentioned algorithm. void printMiddleNode(struct node *head){ /* Input Validation */ if(head == NULL){ printf("Error : Invalid Input !!!!\n"); return INT_MIN; } struct node *slow, *fast; slow = fast = head; while(fast != NULL && fast->next != NULL) { fast = fast->next->next; slow = slow->next; } printf("\nMiddle Node : %d\n", slow->data); }C program to print middle node of a linked list.#include Inserted Element : 3 Inserted Element : 7 Inserted Element : 12 Inserted Element : 5 Inserted Element : 9 Linked List 9-->5-->12-->7-->3 Middle Node : 12 Method 1 C
Algorithm/* Function to find the middle of the linked list */ void findMiddle(node *head) { node *slow_ptr = head; node *fast_ptr = head; if (head!=NULL) { while (fast_ptr != NULL && fast_ptr->link != NULL) { fast_ptr = fast_ptr->link->link; slow_ptr = slow_ptr->link; } printf(" The middle element is %d \n", slow_ptr->data); } }But there is another way which is very efficient. Traverse linked list using two pointers named as fast and slow. Move slow pointer by one node and fast pointer by two node in single iteration. When the fast pointer reaches end slow pointer will reach middle of the linked list. How our program will behave?Our program will take one node as an input and add to linked list. After taking the node program will give options to select options what you want to perform. This program has 4 operations. You can select any one as per your requirement. This 4 operation are:
Program to find middle element of a linked list in single pass#include Output: |