← PayPal bank

PayPal · Question 35 · Linked Lists

Delete Node in a Linked List

Linked ListsJavacode⏱ O(1)💾 O(1)
Product-based

Problem

Delete a given node (except tail) from a singly linked list, given only access to that node.

Approach

Copy the next node's value to current and bypass the next node.

Time O(1)Space O(1)

How it works

  1. 1Overwrite with the successor's value.
  2. 2Bypass the successor.

Sample input / output

Input:
head = [4, 5, 1, 9], node = 5
Output:
[4, 1, 9]

Constraints

2 <= nodes <= 1000; deleted node is never the tail.

Solution — read it, then walk it

Time O(1)Space O(1)Loops 2Returns 2source · Java
Scroll sideways for wide charts — the view follows the active step.
STARTimport java.util.*DEFINE class MainDEFINE class ListNodeDEFINE valListNode nextListNode(int v) { val = v;}DEFINE ListNodebuildList(int[] values)ListNode dummy = newListNode(0), tail = dummyfor (int v : values) {tail.next = newListNode(v); tail …RETURN dummy.nextDEFINE StringtoList(ListNode head)StringBuilder sb = newStringBuilder("[")while (head != null) {sb.append(head.val);head = head.next; i…RETURNsb.append("]").toString()DEFINE static voiddeleteNode(ListNode node)node.val = node.next.valnode.next = node.next.nextDEFINE static voidmain(String[] args)ListNode head =buildList(new int[]{4, 5,1, 9})deleteNode(head.next)System.out.println(toList(head))END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/22

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits