Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was working on a music playlist feature the other day and I initially reached for the standard List<T>. It's the default for a reason—it's fast and familiar. But then I realized the user wanted to be able to drag and drop songs to reorder them, or inject "Recommended" tracks right in the middle of a 5,000-song queue. That's when I stopped and remembered why LinkedList<T> exists.

The hidden cost of the standard List

If you use a List<string> for a playlist, it's backed by an array. When you call Insert(0, "New Song"), C# doesn't just "make room" at the front. It has to physically shift every single existing element one slot to the right in memory. With a few songs, you'll never notice. With thousands? You're wasting CPU cycles on memory copying.

// The "slow" way for large lists
var playlist = new List<string> { "Song A", "Song B", "Song C" };
playlist.Insert(0, "Intro Track"); // Everything shifts right. O(n) complexity.

I wanted something where I could just "snap" a new item into place without bothering the rest of the collection. That's where the doubly linked list comes in.

Trying out the LinkedList

I swapped my List for a LinkedList<string>. At first glance, it felt... different. I tried to access the third song using an index, and the compiler immediately shut me down.

var playlist = new LinkedList<string> { "Song A", "Song B", "Song C" };
// var thirdSong = playlist[2]; // Error! LinkedList doesn't support indexers.

This was my "aha!" moment. A LinkedList isn't a block of contiguous memory; it's a series of LinkedListNode<T> objects scattered around the heap, each holding a reference to the one before it and the one after it. You can't jump to index 2 because the list doesn't know where index 2 is until it starts at the head and follows the "next" pointers.

Navigating via Nodes

To actually manipulate the list, I realized I need to get a handle on a specific node. Since I can't use an index, I used the Find() method to locate a specific song and then used that node as a landmark for insertion.

var playlist = new LinkedList<string> { "Song A", "Song B", "Song C" };

// I want to put "Interlude" right after "Song A"
LinkedListNode<string> targetNode = playlist.Find("Song A");

if (targetNode != null)
{
    playlist.AddAfter(targetNode, "Interlude");
}

// Now the list is: Song A -> Interlude -> Song B -> Song C

This is where the magic happens. Adding "Interlude" didn't require moving "Song B" or "Song C" in memory. C# just updated the Next pointer of "Song A" and the Previous pointer of "Song B" to point to the new node. It's an O(1) operation—constant time, regardless of whether the list has ten songs or ten million.

When to actually use this

You might be thinking, "Why bother with nodes if I can't just use list[i]?" You're right to be skeptical. If your primary goal is reading data randomly, LinkedList is a nightmare because you're always iterating from the start or end.

But if you're building something like a browser history (where you move back and forth), a music queue (where you insert/remove frequently), or a custom undo/redo stack, the LinkedList<T> is your best friend. I use it whenever the "cost of shifting" in a standard list outweighs the "cost of searching" for a node.




📋 Practical Task

The Dynamic Playlist Reorderer

Build a small console application that simulates a music playlist using LinkedList<string>. Your program should perform the following sequence of operations:

  • Initialize a LinkedList<string> with three songs: "Bohemian Rhapsody", "Stairway to Heaven", and "Hotel California".
  • Use the AddFirst() method to add "Intro Theme" to the very beginning of the list.
  • Use the AddLast() method to add "Outro Credits" to the end of the list.
  • Find the node containing "Stairway to Heaven" and use AddAfter() to insert "Imagine" immediately following it.
  • Find the node containing "Hotel California" and use Remove() to delete it from the playlist.
  • Finally, use a foreach loop to print the final order of the playlist to the console to verify the changes.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.