Write a member method remove( ) which randomly generate a position and deletes the node at that position from the linked list.
Node: Please use C++ language, only need function.
/*If you any query do comment in the comment section else like the solution*/
node *remove(node *head) {
node *ptr = head;
node *prev = NULL;
int count = 0;
int random = rand();
while(ptr != NULL) {
if(count == random) {
if(ptr == head) {
ptr = head->next;
head->next = NULL;
delete(head);
head = ptr;
return head;
} else {
prev->next = ptr->next;
delete(ptr);
return head;
}
}
count++;
ptr = ptr->next;
}
return head;
}
Get Answers For Free
Most questions answered within 1 hours.