|
Delete a node from singly linked list given head pointer and pointer of the node .....
Microsoft Interview Questions and Answers
(Continued from previous question...)
273. Delete a node from singly linked list given head pointer and pointer of the node .....
Question:
Delete a node from singly linked list given head pointer and pointer of the node.
Variation: How you delete the node given that you cant delet the node. ex: head->1->2->3->4->NULL. How you delete 3 from list given that you can't do anything to node 3.
Write test cases of all.
maybe an answer:
void deleteOneNode(Node **header, Node *toBeRemoved)
{
if(**header && *header && *toBeRemoved)
{
if(*header == toBeRemoved)
{
*header = toBeRemoved->next;
delete toBeRemoved;
}
else
{
Node *current= *header;
while(current->next && current->next!=toBeRemoved)
{
current=current->next;
}
if(current->next==toBeRemoved)
{
current->next=toBeRemoved->next;
delete toBeRemoved;
}
}
}
}
(Continued on next question...)
Other Interview Questions
|