The function DELETE(head, element) is used to delete a node from the linked list by finding the node value with a given element. The parameter head is the first node of the list. Find the missing statements XX and YY in the following "DELETE" function to delete the node? (Assume all elements are distinct in the list and the function returns pointers that points to the first node of the list.)
Identify the functionality of the following code when function return the value 1. ("head" pointer point to the first node of the non-empty linked list)
int find(Node *head)
{
Node *P1=head, *P2=head;
if(head -> next != NULL)
{
P1=head -> next;
P2=(head -> next)?head -> next -> next:NULL;
}
while((P1!= NULL) && (P2!=NULL)
{
if (P1==P2) return 1;
P1=P1 -> next;
P2= (P2-> next !=NULL)?P2 -> next -> next:NULL;
}
return 0;
}
P1 is slower pointer, which moves to the next node in every iteration of while loop. P2 is faster pointer, which moves to the next to next node in every iteration. If there exist a cycle in the linked list (non-empty), it return 1 by checking P1 == P2. otherwise it return 0.
Assume Node is the structure type with two parameters : 'value' and 'next'. Identify the node printed by the above code if non-empty linked list header is passed to the function find?
P1 traverses node by node. P2 traverses by skipping one node. The node pointed by P1 is the middle node in the linked list when P2 reaches to NULL. Therefore, middle element of the list is printed by P1 -> value.
Both p and q are not NULL then there is a loop in the linked list and it will be detected by slow pointer (p) and fast pointer (q). Therefore, X: (p !=NULL) && (q != NULL)
Let 'temp' be the pointer pointing to the node which needs to be deleted from a doubly linked list. Then find the correct option from the following to delete the node.