PPractick

DSA interview question

Remove Duplicates from a Sorted Doubly Linked List

Hard · Data structures · Topics: linkedlist, doubly linkedlist

The problem

A sensor log keeps its readings in a doubly linked list sorted by value, and repeated readings are noise. The cleanup job must keep one node per distinct value and free the rest, keeping both directions of the list consistent. Because the list is sorted, all copies of a value sit next to each other, so a single forward pass that compares each node with its successor is enough.

Example: 1 <-> 1 <-> 1 <-> 2 <-> 3 <-> 4 → 1 <-> 2 <-> 3 <-> 4.

Function signature

DNode* removeDuplicates(DNode* head);  // keeps the first node of each value; returns the head

Test cases

Input listOutput listExplanation
1 <-> 1 <-> 1 <-> 2 <-> 3 <-> 41 <-> 2 <-> 3 <-> 4A run at the head
1 <-> 2 <-> 2 <-> 3 <-> 3 <-> 4 <-> 41 <-> 2 <-> 3 <-> 4Runs in the middle and at the tail
5 <-> 5 <-> 55All equal
1 <-> 2 <-> 31 <-> 2 <-> 3Nothing to remove
(empty)(empty)

(Confirm: the list is sorted in non-decreasing order; removed nodes are freed; prev pointers must stay correct.)

How interviewers phrase it

Clarifying questions to ask

  1. Is the list sorted? (Yes, so duplicates are adjacent.)
  2. Keep one copy, or remove all copies of repeated values?
  3. Should removed nodes be freed?

Follow-ups to expect

  1. "Why does sorting matter?"
  2. "Why not advance after deleting?"
  3. "What extra work does a doubly linked list need?"
  4. "How would you remove all copies of repeated values?"
  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