Delete a Node in a Linked List Given Only a Pointer to It
viaGlassdoor
Problem: Given a pointer/reference to a node in a singly linked list (not the tail, and without access to the head), delete that node from the list.
Constraints: You do not have access to the head of the list; the node to delete is guaranteed not to be the last node.
Example: List 1->2->3->4, given pointer to node with value 3, after deletion list should behave as 1->2->4.
Approach: Copy the value from the next node into the current node, then point the current node's next to the node after next (effectively skipping it), and free/discard the now-redundant next node. This works because you never actually need to touch the "current" node's original identity, only its data and next pointer. O(1) time.
asked …