*program is in c* Write a function to find the middle node in a linked list and
ID: 3831660 • Letter: #
Question
*program is in c*
Write a function to find the middle node in a linked list and return a pointer to the middle node. Function prototype is given below. node *middlenode(node *list1) If the list has 2k + 1 elements where k is an integer greaterthanorequalto 0, return the (k + 1)^th node from the start. If the list has 2k elements where k is an integer greaterthanorequalto 0, return the (k + 1)^th node from the start. For the following linked list the function returns a pointer to node containing 12.Explanation / Answer
Please find my implementation.
Please let me know in case of any issue.
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to get the middle of the linked list*/
node* middlenode(struct node *list1)
{
struct node *slow_ptr = list1;
struct node *fast_ptr = list1;
if (head!=NULL)
{
while (fast_ptr != NULL && fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
slow_ptr = slow_ptr->next;
}
if(fast_ptr->next == NULL){ // even number of nodes
slow_ptr = slow_ptr->next; //(2k+1)th node
}
return slow_ptr;
}
return NULL;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.