androidengineers.Book a session

Linked Lists

Use Cases: Undo Feature, Music Playlists, Hash Chains

article20 minEasy

Where do linked lists actually shine in production software? Let's analyze three core architectural implementations where pointer-based nodes provide the ideal solution.


1. Music Playlist Playback Engine

The Problem

A music streaming app (Spotify, YouTube Music) needs an active playlist queue:

  • Play next track / play previous track.
  • Reorder songs via drag-and-drop.
  • Toggle "Repeat All" (looping back to the start).

Why a Circular Doubly Linked List is Ideal

  • current.next plays the next song in O(1).
  • current.prev returns to the previous song in O(1).
  • Dragging a song to a new position only updates 4 pointers (O(1) relink), compared to an array which would require shifting thousands of items.
  • Tail connects to Head, enabling seamless repeat playback.
class Track(val id: String, val title: String)

class Playlist {
    class SongNode(val track: Track, var prev: SongNode? = null, var next: SongNode? = null)
    
    private var currentSong: SongNode? = null
    
    fun playNext(): Track? {
        currentSong = currentSong?.next
        return currentSong?.track
    }
    
    fun playPrevious(): Track? {
        currentSong = currentSong?.prev
        return currentSong?.track
    }
}

2. Hash Table Collision Resolution (Separate Chaining)

The Problem

When two different keys produce the same hash bucket index in a HashMap, both entries must be stored without overwriting each other.

The Linked List Solution

Each bucket in a hash table array acts as the head of a singly linked list:

Bucket Array:
[ 0 ] ---> [ "Alice" : 95 ] ---> [ "Bob" : 88 ] ---> null  (Collision chain!)
[ 1 ] ---> null
[ 2 ] ---> [ "Charlie" : 92 ] ---> null

Inserting a collided key takes O(1) time by prepending a new node at the head of the bucket list.


3. Undo / Redo Command History

The Problem

In an image editing or note-taking application, every user action produces a state snapshot or command. Users can undo backwards or redo forwards. When a new action is performed after an undo, future history must be truncated.

The Doubly Linked List Implementation

Action 1 <=====> Action 2 <=====> Action 3 (Current Pointer)
  • When the user presses Undo, current = current.prev.
  • When the user presses Redo, current = current.next.
  • When a New Action is applied while sitting at Action 2, you set current.next = null and attach the new node. All subsequent history is pruned in O(1)!

Summary

ApplicationChosen Linked StructureKey Architectural Benefit
Music PlaylistCircular Doubly Linked ListInstant bidirectional step & O(1) track reordering
HashMap ChainingSingly Linked ListMemory allocated strictly on demand per collision
Undo / Redo BufferDoubly Linked ListO(1) history truncation and forward/back navigation

YOUR LEARNING JOURNEY

0 of 55 available lessons completed

Progress saved in this browser. No account needed.
Use Cases: Undo Feature, Music Playlists, Hash Chains | Data Structures | Android Engineers