When should I use a List vs a LinkedList

后端 未结 15 2137
挽巷
挽巷 2020-11-22 14:50

When is it better to use a List vs a LinkedList?

15条回答
  •  花落未央
    2020-11-22 15:31

    I do agree with most of the point made above. And I also agree that List looks like a more obvious choice in most of the cases.

    But, I just want to add that there are many instance where LinkedList are far better choice than List for better efficiency.

    1. Suppose you are traversing through the elements and you want to perform lot of insertions/deletion; LinkedList does it in linear O(n) time, whereas List does it in quadratic O(n^2) time.
    2. Suppose you want to access bigger objects again and again, LinkedList become very more useful.
    3. Deque() and queue() are better implemented using LinkedList.
    4. Increasing the size of LinkedList is much easier and better once you are dealing with many and bigger objects.

    Hope someone would find these comments useful.

提交回复
热议问题