← Uber bank

Uber · Question 24 · Linked Lists

Delete node in a linked list

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

Problem

Delete the given node (except tail) from a singly linked list.

Approach

Copy the next node’s value and pointer.

Time O(1)Space O(1)

How it works

  1. 1Copy the next node's value into the target.
  2. 2Bypass the next node.
  3. 3Works because the node is never the tail.

Sample input / output

Input:
4
4 5 1 9
1
Output:
[4, 1, 9]

Constraints

2 <= nodes <= 1000; node to delete is not the tail.

Solution — read it, then walk it

Time O(1)Space O(1)Loops 3Branches 1Returns 2source · Python
Scroll sideways for wide charts — the view follows the active step.
↻ repeat↻ repeat↻ repeatyesnoSTARTnodenode.val = node.next.valnode.next = node.next.nextCLASS ListNodeDEFINE __init__(self,val=0, next=None)self.val = valself.next = nextDEFINE build_list(values)dummy = tail = ListNode()for v in valuestail.next = ListNode(v)tail = tail.nextRETURN dummy.nextDEFINE to_list(head)out = []while headout.append(head.val)head = head.nextRETURN outDEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)n = int(next(it))values = [int(next(it)) for_ in range(n)]idx = int(next(it))head = build_list(values)nodes = [head]while nodes[-1].nextnodes.append(nodes[-1].next)deleteNode(nodes[idx])print(to_list(head))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/35

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