You currently have O (1) for insertion and O (nlogn) for getMin/getMax/getMedian . You can move the log file from the getters to the insertion part using a sorted data structure. Or you can leave the insert as it is and optimize getMin/getMax by doing a linear search on the list and just keeping the minimum value. For getMedian you have nothing to do, as you need a sorted set for this.
Further optimization will consist in saving min / max and updating two values ββat each insertion stage. This will not have a (not permanent) change on insertion and will reduce your getMin/getMax to O (1). (thanks to Tedil )
The same goes for getMedian , where you will store a sorted list in parallel with your linked list. Then you can simply select the median middle of this list. Of course, this will change the insert time to O (logn) or worse (depending on the list you use), and also double the amount of storage space. Thus, this is a more expensive optimization than for getMin/getMax .
source share