PPractick

DSA interview question

Delete the Head of a Linked List (and a Node at a Position)

Easy · Data structures · Topics: linkedlist

The problem

When the music player finishes a song, it must remove the head of the queue: the second song becomes the new head, and the old head's memory is freed. The same code base also removes a song at position k when the user swipes it away. Deleting the head is O(1): remember the old head, move head to head->next, free the old node. Deleting at position k needs the predecessor, so its next can skip over the removed node. As with insertion, the empty list and head deletion are the edge cases to get right.

Example: 1 -> 2 -> 3, delete the head → 2 -> 3.

Function signature

Node* deleteHead(Node* head);                   // returns the new head (nullptr if the list becomes empty)
Node* deleteAtPosition(Node* head, int pos);    // pos is 1-indexed and valid

Test cases

InputOutput listExplanation
deleteHead(1 -> 2 -> 3)2 -> 3
deleteHead(5)(empty)The only node is removed
deleteHead(empty)(empty)Nothing to delete
deleteAtPosition(1 -> 2 -> 3 -> 4, 3)1 -> 2 -> 4The middle node is removed
deleteAtPosition(1 -> 2, 1)2Position 1 is the head
deleteAtPosition(1 -> 2 -> 3, 3)1 -> 2The tail

(Confirm: positions are 1-indexed and valid; removed nodes are freed; the function returns the new head.)

How interviewers phrase it

Clarifying questions to ask

  1. What should happen for an empty list?
  2. Are positions 0- or 1-indexed?
  3. Should deleted nodes be freed?

Follow-ups to expect

  1. "Why do you need the predecessor to delete a middle node?"
  2. "How do you delete a node given only a pointer to it (not the tail)?"
  3. "How do you delete the tail?"
  4. "How does a dummy head help?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in