C# › Collections
How would you choose between List<T>, LinkedList<T>, and ImmutableList<T> based on their different Big-O characteristics for common operations?
AdvancedAnswer
List<T> offers O(1) index access and O(1) amortized append, but O(n) insertion/removal in the middle (due to shifting elements); LinkedList<T> offers O(1) insertion/removal ANYWHERE (given a node reference), but O(n) index access (no random access at all); ImmutableList<T> uses a persistent tree structure giving O(log n) for most operations while preserving full immutability (every 'mutation' returns a new list sharing most of the old structure).
List<int> list = new(); list.Insert(0, 1); // O(n): shifts every existing element
LinkedList<int> linked = new(); linked.AddFirst(1); // O(1): no shifting needed
ImmutableList<int> immutable = ImmutableList<int>.Empty.Add(1); // O(log n), returns a NEW listReal-world example Choosing LinkedList<T> specifically for a scenario with frequent insertions/removals at arbitrary positions and no need for index access, like an LRU cache's internal ordering.