Sollicitatievraag bij Thomson Reuters

What is the difference between a LinkedList and ArrayList

Antwoord op sollicitatievraag

Anoniem

8 aug 2016

ArrayList stores elements in consecutive memory locations, wherease LinkedList doesn't (it may store elements at different memory locations and next element is referred by a link). It takes O(N) time complexity for locating a specific location in LinkedList, however it is O(1) in case of ArrayList. Deleting and inserting an element is O(N) for ArrayList, however it is O(1) for LinkedList. LinkedList has overhead of storing previous (and next) pointers. When to use one over the other: 1) As explained above the insert and remove operations give good performance (O(1)) in LinkedList compared to ArrayList(O(n)). Hence if there is a requirement of frequent addition and deletion in application then LinkedList is a best choice. 2) Getting element at specific index is fast in Arraylist (O(1)) but not in LinkedList (O(n)) so If there are less add and remove operations and more index access operations requirement, ArrayList would be your best bet.