oneby7

Linked Lists Doubly linked lists

280. Delete the Head of a Doubly Linked List

Medium

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

Each node has a value, val, and two pointers. next points to the following node, and prev points to the node before it. The head's prev is null, and the last node's next is null. The list you return must keep both directions right. The comment at the top of the starter code shows the node in your language.

If the list has one node, deleting it leaves the empty list, whose 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, and the output shows the list you return the same way, with null as []. The judge also checks every prev pointer. If one is wrong, the output names the first node whose prev is wrong, counting the head as node 1, instead of the values.

Example 1

Input: head = [3, -8, 14, 0]

Output: [-8,14,0]

Explanation: The node holding 3 is the head, so it goes. The node holding -8 becomes the head, and its prev is set to null, since no node comes before it now.

Example 2

Input: head = [-1000000000]

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.