AlgoViz

Delete head of Doubly Linked List

Easy

Advance the head, clear its prev

Problem

Given the head of a doubly linked list, delete the head node and return the new head (its 'prev' must be null).

In simple words

Move 'head' to the second node and clear its back-pointer.

The idea

Move the head to head.next and set that node's prev to null so it becomes a proper head. Leaving a stale prev pointer creates a list that appears to have a node before its own head.

The trick

  • Always null the new head's prev.
  • If the list had one node, the result is an empty list — return null.

This one walks through the worked example rather than tracing the algorithm frame by frame — a full walkthrough is still to be drawn. The code and the idea below are the real solution.

1
2
3
0
1
2

Step 1 of 2. Here's the example — head = [1, 2, 3] Values: 1, 2, 3.

1/2
Optimal
timeO(1)spaceO(1)
1if head == null: return null2head = head.next3if head != null: head.prev = null4return head

Input

array
[1, 2, 3]

Output

answer

Check yourself

3 quick questions about this walkthrough. A wrong answer costs nothing.

Example

Input:
head = [1, 2, 3]
Output:
[2, 3]

Practice this problem:GeeksforGeeks(opens in a new tab)

Finished the walkthrough? Add it to your streak.