Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Remove Duplicates from List

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

Your question is Remove Duplicates from 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 Semiconductor diagnostic pipelines may represent ordered event identifiers as a singly linked list. Implement an in-place algorithm that removes every duplicate node while preserving the first occurrence and the relative order of remaining nodes.

Use the following node model:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

Implement remove_duplicates(head). The function receives the head node and must return the head of the modified list. A linked list such as [3, 1, 3, 2, 1] is represented in examples and tests by its serialized value array, but the submitted function operates on ListNode objects.

Do not allocate replacement nodes or use a set, dictionary, list, or other value-based buffer. Rewire existing next pointers directly. Keep the first node containing each value.

Constraints

  • 0 <= number of nodes <= 10^4
  • -10^9 <= node.val <= 10^9
  • The input is a singly linked list of ListNode objects
  • Do not use auxiliary storage proportional to the number of nodes

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