Goldman Sachs Interview Question: How to remove a node from a s... | Glassdoor

Interview Question

Software Engineer Interview New York, NY

How to remove a node from a singly-linked list when only

  given the pointer to the node
Tags:
programming, c, pointers
Answer

Interview Answer

6 Answers

0

You have to use some pointer/memory trickery

Interview Candidate on Jul 31, 2010
12

thisNode.data = thisNode.next.data
temporaryNode = thisNode.next.next
delete thisNode.next
thisNode.next = temporaryNode

jobseeker on Aug 31, 2010
1

The post by "jobseeker" assumes that we know the node BEFORE the node to be deleted. This assumption is not consistent with the problem definition, but makes it solvable.

Milly on Oct 21, 2010
1

Milly, jobseeker's assumption is reasonable, we should not delete the node before removing it from the list (if it was created by the list)

Anonymous on Oct 25, 2010
2

Milly: jobseeker is right in that you don't delete the "node to be deleted". You delete the node that FOLLOWS the "node to be deleted", only after swapping the values of the two.

anon on Mar 17, 2011
1

node ->val=node->next->val;
     node ->next=node->next->next;

David Dhas on Mar 7, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.