Problem LeetCode 56. Merge Intervals ↗ · NeetCode ↗· The solutions on this page are my own.

Merge Intervals Visualizer

Three solutions to LeetCode 56, run step by step next to their Java code. Touching intervals such as [1,4] and [4,5] count as overlapping.

The problem, step by step

Skip to the visualizer ↓

Merge every group of overlapping ranges into one

You get a list of ranges, each with a start and an end. Some of them overlap. Combine the ones that overlap, so that no two ranges in the answer share a point.

  1. What you get

    An array intervals where each item is a pair [start, end] with start ≤ end. The list is in no particular order.

  2. What you return

    A list of ranges that covers exactly the same points as the input, with every overlapping group merged into one range. No two ranges in the result overlap. Sorted by start is the natural order to return them in.

  3. The rules

    • Take the two ranges in order of their start. They overlap when the second one starts before or exactly when the first one ends. [1,4] and [4,5] touch at 4, so they merge into [1,5].
    • Merging can chain: if A overlaps B and B overlaps C, all three become one range, even when A and C never meet.
    • A merged range runs from the smallest start in its group to the largest end.
  4. Try it by hand

    Input
    [[6,8], [1,3], [2,4], [9,12], [10,11]]
    Output
    [[1,4], [6,8], [9,12]]
    1. Draw every range on a number line. Once they are drawn, the order of the list no longer matters.
    2. [1,3] and [2,4] share the stretch from 2 to 3, so they become one block from 1 to 4.
    3. [6,8] touches nothing: the block before it ends at 4, and the next range starts at 9. It stays as it is.
    4. [10,11] lies completely inside [9,12], so it adds nothing. The block stays [9,12].
    5. Read the blocks from left to right: [[1,4], [6,8], [9,12]].
  5. Edge cases to check

    • Touching ends merge: [[1,4],[4,5]] gives [[1,5]].
    • A range inside another keeps the larger end: [[1,10],[2,3]] gives [[1,10]], not [[1,3]].
    • A chain becomes one range: [[1,3],[2,5],[4,8]] gives [[1,8]].
    • No overlap at all: [[8,9],[1,2]] gives both ranges back.
    • A single range comes back unchanged.
  6. The question behind the solutions

    On paper the number line sorts the ranges for you. In code the list arrives shuffled, and comparing every range with every other one costs n² comparisons, while a merged range can grow and start to overlap new ones. In what order should you look at the ranges, so that each one is decided once?

Three ways to merge

The example from above, [[6,8], [1,3], [2,4], [9,12], [10,11]] with the ranges named A to E in input order, sketched the way you would draw it in an interview. All three end with [[1,4], [6,8], [9,12]]. Then step through them below.

1. Sort + scan MergeIntervalsSort

Remembers: the ranges sorted by start, and the block being built.

sorted by startB [1,3]start with [1,3]C [2,4]2 ≤ 3: grow to 4A [6,8]6 > 4: emit [1,4]D [9,12]9 > 8: emit [6,8]E [10,11]10 ≤ 12: inside04812[1,4][6,8][9,12]end: emit [9,12]once sorted, each range is decided in one look
  1. Sort the ranges by start.
  2. If the next range starts at or before the current end, grow the end to the larger of the two.
  3. Otherwise emit the current block and start a new one. Emit the last block at the end.

The catch: the sort costs O(n log n). After it, every range is looked at once.

2. Overlap graph MergeIntervalsGraph

Remembers: a node per range and an edge for every overlapping pair.

a node per range, an edge when two overlapB[1,3]C[2,4]A[6,8]D[9,12]E[10,11]overlap: a.start ≤ b.end and b.start ≤ a.end10 pairs checked, 2 edges foundA → [6,8]B, C → [1,4]D, E → [9,12]each connected group becomes one range
  1. Check every pair: they overlap when a.start ≤ b.end and b.start ≤ a.end.
  2. Run a DFS from each range not visited yet to find its connected group.
  3. Each group becomes one range: its smallest start to its largest end.

The catch: n² ÷ 2 pair checks and up to n² edges, and the groups come out in input order, so you sort them again if you need order.

3. Sweep line MergeIntervalsSweepLine

Remembers: 2n events and a count of open ranges.

+1+1+1+1+1−1−1−1−1−121open024681012[1,4][6,8][9,12]count back to 0: emit the block
  1. Turn each range into two events: +1 at its start, −1 at its end.
  2. Sort the events by position, with starts before ends at the same point, so touching ranges merge.
  3. A block starts when the count leaves 0 and is emitted when it falls back to 0.

The catch: it is O(n log n) like the sort, with twice as many items to sort. It pays off when you also need how many ranges overlap at each point.

Three ways to solve it, step by step

Algorithm
Input
start,end pairs, e.g. 1,3 2,6 8,10 (1 to 12 intervals, values 0 to 99)

0merged intervals
0
0

not merged yet colored by merged interval current open scan line

lines that run are highlighted

      

Complexity · n intervals

time
extra space

All three compared

VariantTimeExtra spaceSorts?Idea

Related in the Lab sorting and lists in the Java Collections Field Guide

© 2026 Arman Ajabkhani · armantik.com · Privacy