Finding the Middle Node
Procedure to find the middle node of a singly linked list.
First Method : Finding Length & Then Middle Element
int findMid(struct node *ptr) {
int len = 0;
// Finding the length of linked list
while (ptr != NULL) {
len++;
ptr = ptr -> next;
}
// Find the middle position
int mid = ceil(len/2);
// Resetting ptr to head
ptr = head;
// Return the middle element
for (int i = 0; i < mid; i++) {
ptr = ptr -> next;
}
return ptr -> data;
}Second Method : Using Two Pointers
Last updated