Linked Lists · Coding problem

Delete Node in a Linked List

A free coding problem from the Singly Linked List Basics skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

You're handed one node of a singly linked list and nothing else: no head, and no way to walk back. The node is never the last one. Remove it from the list. There's nothing to return; change the list so that node's value no longer appears where it was.

Input format

Line 1: n, the number of nodes. Line 2: n values. Line 3: the 0-indexed position of the node to delete (never the last position).

Example

Input:
6
3 8 6 2 7 4
2
Output:
3 8 2 7 4

The node at position 2, holding 6, is gone.

Since you can't reach the node before it, you can't unlink this node the usual way. What can you change about the node you were given?

Constraints

  • 2 <= number of nodes <= 1000
  • -10^5 <= node value <= 10^5
  • The node to delete is not the last node in the list

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Singly Linked List Basics

Solve it Delete Node in a Linked List free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime