Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals.

**Solution**

Python 3

```
def mergeIntervals(intervals):
# Sort the array on the basis of start values of intervals.
intervals.sort()
stack = []
# insert first interval into stack
stack.append(intervals[0])
for i in intervals[1:]:
# Check for overlapping interval,
# if interval overlap
if stack[-1][0] <= i[0] <= stack[-1][-1]:
stack[-1][-1] = max(stack[-1][-1], i[-1])
else:
stack.append(i)
print("The Merged Intervals are :", end=" ")
for i in range(len(stack)):
print(stack[i], end=" ")
```