Keep One Representative of Each Sorted List Value

Quick Overview

Collapse each equal-value run of a sorted linked list to one representative while retaining the pointer interface.

Keep One Representative of Each Sorted List Value

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given the head of a sorted singly linked list, remove excess copies so each distinct value appears once. Return the head of the resulting sorted linked list. A value that appeared repeatedly must still have one representative. The supplied `ListNode` has `val` and `next` fields: ```cpp ListNode* deleteDuplicates(ListNode* head); ``` Brackets in examples display values along the pointer chain. ### Examples ```text Input: head = [-3,-3,0,0,0,5] Output list: [-3,0,5] ``` ```text Input: head = [7,7,7] Output list: [7] ``` ### Constraints - There are between 0 and 300 nodes. - Every node value is between -100 and 100, inclusive. - The input list is sorted in ascending order, allowing equal values.

Overview: Collapse each equal-value run of a sorted linked list to one representative while retaining the pointer interface.

Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given the head of a sorted singly linked list, remove excess copies so each distinct value appears once. Return the head of the resulting sorted linked list. A value that appeared repeatedly must still have one representative.

The supplied ListNode has val and next fields:

ListNode* deleteDuplicates(ListNode* head);

Brackets in examples display values along the pointer chain.

Examples

Input: head = [-3,-3,0,0,0,5]
Output list: [-3,0,5]
Input: head = [7,7,7]
Output list: [7]

Constraints

  • There are between 0 and 300 nodes.
  • Every node value is between -100 and 100, inclusive.
  • The input list is sorted in ascending order, allowing equal values.