oneby7

Linked Lists Singly linked list basics

268. Delete the Head

Medium

You are given head, the first node of a singly linked list. Delete the first node and return the head of the list that is left.

A singly linked list is a chain of nodes. Each node holds a value and a pointer to the next node, and the last node's pointer is null. An empty list has no nodes, and its head is null. If the list is empty, there is nothing to delete: return the empty list.

The examples write a list as its values from the head on, so head = [4,1,6] is the list 4 → 1 → 6. The output shows the node you return the same way, as the list from that node on, and shows null as [].

Example 1

9-4126
head
-4126
output

Input: head = [9, -4, 12, 6]

Output: [-4,12,6]

Explanation: The node holding 9 is the head, so it goes. The node holding -4 was second, and now it is the head of the list that is left.

Example 2

42

Input: head = [42]

Output: []

Explanation: The only node is the head. Once it is deleted, the list is empty, so the answer is null.

Example 3

Input: head = []

Output: []

Explanation: The list is empty, so there is no node to delete, and the list stays empty.

Check your understanding

Work out the answer for this input before you write any code.

head = [5, 8, 1]

Constraints

  • 0 ≤ number of nodes ≤ 105
  • -109 ≤ Node.val ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.