When is it better to use a List vs a LinkedList?
The difference between List and LinkedList lies in their underlying implementation. List is array based collection (ArrayList). LinkedList is node-pointer based collection (LinkedListNode). On the API level usage, both of them are pretty much the same since both implement same set of interfaces such as ICollection, IEnumerable, etc.
The key difference comes when performance matter. For example, if you are implementing the list that has heavy "INSERT" operation, LinkedList outperforms List. Since LinkedList can do it in O(1) time, but List may need to expand the size of underlying array. For more information/detail you might want to read up on the algorithmic difference between LinkedList and array data structures. http://en.wikipedia.org/wiki/Linked_list and Array
Hope this help,
Linked lists provide very fast insertion or deletion of a list member. Each member in a linked list contains a pointer to the next member in the list so to insert a member at position i:
The disadvantage to a linked list is that random access is not possible. Accessing a member requires traversing the list until the desired member is found.
Use LinkedList<>
when
Token Stream
.For everything else, it is better to use List<>
.
The primary advantage of linked lists over arrays is that the links provide us with the capability to rearrange the items efficiently. Sedgewick, p. 91
Thinking of a linked list as a list can be a bit misleading. It's more like a chain. In fact, in .NET, LinkedList<T>
does not even implement IList<T>
. There is no real concept of index in a linked list, even though it may seem there is. Certainly none of the methods provided on the class accept indexes.
Linked lists may be singly linked, or doubly linked. This refers to whether each element in the chain has a link only to the next one (singly linked) or to both the prior/next elements (doubly linked). LinkedList<T>
is doubly linked.
Internally, List<T>
is backed by an array. This provides a very compact representation in memory. Conversely, LinkedList<T>
involves additional memory to store the bidirectional links between successive elements. So the memory footprint of a LinkedList<T>
will generally be larger than for List<T>
(with the caveat that List<T>
can have unused internal array elements to improve performance during append operations.)
They have different performance characteristics too:
LinkedList<T>.AddLast(item)
constant timeList<T>.Add(item)
amortized constant time, linear worst caseLinkedList<T>.AddFirst(item)
constant timeList<T>.Insert(0, item)
linear timeLinkedList<T>.AddBefore(node, item)
constant timeLinkedList<T>.AddAfter(node, item)
constant timeList<T>.Insert(index, item)
linear timeLinkedList<T>.Remove(item)
linear timeLinkedList<T>.Remove(node)
constant timeList<T>.Remove(item)
linear timeList<T>.RemoveAt(index)
linear timeLinkedList<T>.Count
constant timeList<T>.Count
constant timeLinkedList<T>.Contains(item)
linear timeList<T>.Contains(item)
linear timeLinkedList<T>.Clear()
linear timeList<T>.Clear()
linear timeAs you can see, they're mostly equivalent. In practice, the API of LinkedList<T>
is more cumbersome to use, and details of its internal needs spill out into your code.
However, if you need to do many insertions/removals from within a list, it offers constant time. List<T>
offers linear time, as extra items in the list must be shuffled around after the insertion/removal.
A common circumstance to use LinkedList is like this:
Suppose you want to remove many certain strings from a list of strings with a large size, say 100,000. The strings to remove can be looked up in HashSet dic, and the list of strings is believed to contain between 30,000 to 60,000 such strings to remove.
Then what's the best type of List for storing the 100,000 Strings? The answer is LinkedList. If the they are stored in an ArrayList, then iterating over it and removing matched Strings whould take up to billions of operations, while it takes just around 100,000 operations by using an iterator and the remove() method.
LinkedList<String> strings = readStrings();
HashSet<String> dic = readDic();
Iterator<String> iterator = strings.iterator();
while (iterator.hasNext()){
String string = iterator.next();
if (dic.contains(string))
iterator.remove();
}