Doubly Linked List Implementation in C#
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Doubly Linked List runs in O(1) insert time and O(n) space, in C# as in every other language.
- ✓System.Collections.Generic.LinkedList<T> together with LinkedListNode<T>. It is the only collection in the base class library that hands you the node itself, which makes it the only one where removal in constant time is reachable from your own code.
- ✓Pair it with a dictionary from key to LinkedListNode<T>. That combination is the standard .NET LRU cache and is the reason LinkedList<T> exists in the framework at all: the dictionary answers "where is it" in O(1) and the node answers "unlink it" in O(1).
- ✓The C# trap: Remove is overloaded: Remove(LinkedListNode<T>) is O(1) and Remove(T value) is an O(n) scan.
- ✓Each node is a class instance, so a 16-byte object header plus three references plus the value, and they are scattered across the managed heap.
What Doubly Linked List actually is
Doubly Linked List in C#: what you actually reach for
C# implementation
using System.Collections.Generic;
// The canonical .NET LRU: dictionary for lookup, linked list for order,
// and the node itself as the bridge that makes both halves O(1).
public sealed class LruCache<TKey, TValue> where TKey : notnull
{
private readonly int _capacity;
private readonly LinkedList<(TKey Key, TValue Value)> _order = new();
private readonly Dictionary<TKey, LinkedListNode<(TKey Key, TValue Value)>> _index;
public LruCache(int capacity)
{
_capacity = capacity;
_index = new Dictionary<TKey, LinkedListNode<(TKey, TValue)>>(capacity);
}
public bool TryGet(TKey key, out TValue value)
{
if (!_index.TryGetValue(key, out var node))
{
value = default!;
return false;
}
_order.Remove(node); // O(1): the NODE overload, not the value overload
_order.AddFirst(node); // reuse the same node object, no allocation
value = node.Value.Value;
return true;
}
public void Put(TKey key, TValue value)
{
if (_index.TryGetValue(key, out var existing))
{
_order.Remove(existing);
existing.Value = (key, value);
_order.AddFirst(existing);
return;
}
if (_index.Count >= _capacity)
{
var last = _order.Last!;
_order.RemoveLast();
_index.Remove(last.Value.Key);
}
_index[key] = _order.AddFirst((key, value));
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe C#-specific trap
Performance in C#, beyond the O(1) insert
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which C# type or library should I use for Doubly Linked List?
System.Collections.Generic.LinkedList<T> together with LinkedListNode<T>. It is the only collection in the base class library that hands you the node itself, which makes it the only one where removal in constant time is reachable from your own code. AddAfter, AddBefore, AddFirst, AddLast and Remove all take or return the node.
What is the most common C# mistake when implementing Doubly Linked List?
Remove is overloaded: Remove(LinkedListNode<T>) is O(1) and Remove(T value) is an O(n) scan. The two are chosen by overload resolution, so passing a value where you meant a node changes the complexity of your cache silently and compiles without a warning. Separately, a LinkedListNode<T> belongs to exactly one list for its whole life: passing a node that is already in another list, or has been removed, to AddAfter throws InvalidOperationException at run time.
How does Doubly Linked List actually perform in C#?
Each node is a class instance, so a 16-byte object header plus three references plus the value, and they are scattered across the managed heap. Because .NET generics are reified rather than erased, LinkedList<int> stores real ints with no boxing, which is a concrete advantage over the equivalent Java code where every element becomes an Integer object. Time complexity is O(1) insert and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Replit, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.