===== ISTEM =====
Solve the following programming task.

TASK: Merge Intervals

Write a function 'merge_intervals(intervals)' that takes a list of closed intervals that may overlap (e.g. [[1,3],[2,6],[8,10],[15,18]]) and returns a new list with the overlapping ones merged.

STARTER CODE (python):
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
    # Write your code here
    pass


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```python
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
    if not intervals:
        return []
    sorted_intervals = sorted(intervals, key=lambda x: x[0])
    merged = [list(sorted_intervals[0])]
    for start, end in sorted_intervals[1:]:
        if start <= merged[-1][1]:
            merged[-1][1] = max(merged[-1][1], end)
        else:
            merged.append([start, end])
    return merged
```