Problem 299072 · medium · Level 02 Linear Data Structures

Merge Sorted Lists Without Repeats

arrays · two pointers · merging

Given two lists a and b, each sorted in non-decreasing order (each may contain repeated values), return a new sorted list that contains every value occurring in either list exactly once.

Examples

Input:  a = [1, 2, 2, 5], b = [2, 3, 5, 7]
Output: [1, 2, 3, 5, 7]

Input:  a = [1, 1, 1], b = [1]
Output: [1]

Constraints

  • 0 <= len(a), len(b) <= 10**5
  • Return a new list.
  • Target: O(len(a) + len(b)) time; do not call sorted or use a set.

Goals

  • Merge two sorted lists with two indices
  • Skip values equal to the last value written
  • Handle duplicates inside a single list as well as across lists
Starting Python…