Problem · Linked List

Delete a Linked-List Node Without the Head

Learn this problem
MediumCapillary Technologies logoCapillary TechnologiesFULLTIMEONSITE INTERVIEW

Problem statement

You are given only a node node from a singly linked list. You are not given the list's head. Delete node from the list in place.

The provided node is guaranteed not to be the tail. Copy the successor's value into node, bypass that successor, and return node. The returned list therefore represents the suffix beginning at the original target position after deletion.

Function

deleteNode(node: ListNode) → ListNode

Examples

Example 1

node = [5,1,9]return = [1,9]

The target contains 5. Copy 1 from its successor into the target, then bypass that successor.

Example 2

node = [7,3]return = [3]

The target takes its successor's value 3 and becomes the last node.

Constraints

  • The provided node is not null.
  • The provided node is not the tail node.
  • The serialized suffix contains between 2 and 10^5 nodes.
  • Each node value fits in a 32-bit signed integer.

More Capillary Technologies problems

drafts saved locally
public ListNode deleteNode(ListNode node) {
    // write your code here
}
node[5,1,9]
expected[1,9]
checking account