===== 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 []
    
    intervals.sort(key=lambda x: x[0])
    merged = [intervals[0]]
    
    for current in intervals[1:]:
        last = merged[-1]
        if current[0] <= last[1]:
            last[1] = max(last[1], current[1])
        else:
            merged.append(current)
            
    return merged
```