简体   繁体   中英

Time complexity of inserting a node in a sorted linked list

What is the time complexity of inserting a node in a sorted linked list in Java? Is there an algorithm with complexity less than O(n) ?

If all you have is a linked links and you're starting from the head, in the worst case you have to iterate over the entire list to find the insertion point. This gives O(n) worst-case time.

Something like a skiplist could give O(log n) insertion. However, that's a different data structure to what you're asking about (and so are trees etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM