Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Remove Duplicate Nodes in Linked List

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Remove Duplicate Nodes in Linked List. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Samsung device software may store ordered event identifiers in a singly linked list. Given the head of the list, remove every node whose value has appeared earlier, preserving the first occurrence and the original order of remaining nodes.

The removal must be performed in place: do not create replacement nodes and do not use a hash set, dictionary, array, or other collection to track values. Return the original head of the modified list, or None if the list is empty.

Formal Specification

The input is head, either None or a reference to a singly linked list whose nodes have integer field val and pointer field next. The function returns the head reference of the deduplicated list. For examples and test cases, lists are shown as arrays, and the evaluator converts them to linked-list nodes before calling the function.

Constraints

  • 0 <= n <= 10^4
  • -10^9 <= node.val <= 10^9
  • The input is a singly linked list
  • The relative order of retained nodes must remain unchanged
  • No auxiliary collection may be used

Function Signature

def remove_duplicates(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output