It's really funny to see this as I had to solve something really similar but with angle ranges instead. And since they are angles, the ranges looped around which made for an interesting problem to think about.

I managed to come up with a pretty elegant solution by adding each interval one by one to a single dimension sorted list. (Keeping in mind that the opening value could very well end up at the end of the list since a range such as [350, 20] can exist when intervals loop around.)

Then it's only a matter of removing all the indexes between the intervals that you are adding. (If the opening value or closing value itself is within an other existing interval, simply don't add it, but still remove everything between the indexes where it would have been added.)

Now the cool thing with this is that based on how you break the intervals back out of that list, your code will either have added, or removed intervals.

Example:

[1, 4] [2, 5] -> [1, 5] or [5, 1]

Or with more:

[1, 4] [2, 5] [6, 9] - > [1, 5] [6, 9] or [5, 6] [9, 1]