Pergunta de entrevista da empresa Palantir Technologies

Given a pointer to an element in a singly linked-list, how can you delete the element from the list?

Respostas da entrevista

Sigiloso

20 de set. de 2015

the tricky part in thw questions is that you dont have any pointer to the root of the list. You only have access to the element you want to remove. So you have to copy the data from the next node, element.data=element.next.data and then element.next=element.next.next in doing this you "removed" the element from the list. I hope this is clear

3

Sigiloso

10 de set. de 2015

Copy the data from the node ahead of the element that has to be deleted and make the current element poi to next->next.

1

Sigiloso

20 de set. de 2015

Since we have pointer to the element we want to delete and can't go back to it's parent we need to walk the List from the beginning and check current->next == selectedNode once we find a match then all we have to do is current->next = selectedNode->next an example: ROOT > A > B > C > D > E > NULL selectedNode = C while(node != null) { if (node->next == selectedNode) { node->next = selectedNode->next } node = node.next }