Skip to the workspace
Pebellslearn by building
Linked Lists, Stacks & Queues  ›  Lesson

Linked Lists & the Linked Playlist

Linked Lists 25 minNodes joined by references: cheap splicing, no random access

dsa / Linked Lists & the Linked Playlist

Explore each scene at your own pace.

01 · Splice a track into a 5,000-track party mix

Splice a track into a 5,000-track party mix

Linked Lists & the Linked Playlist

Definition
A linked list is a chain of nodes, each holding a value and a reference to the next node, so you reach items by following links from the head and can insert or remove next to a node you hold in O(1) time.
In plain words
Think of a treasure hunt where each clue tells you where the next one is hidden. There is no numbered shelf to jump to, but adding a new clue only means changing what the previous clue points at.

In short: a Python list is an array — items sit side by side in one block, which is why tracks[i] is O(1) but inserting near the front means shifting everything after it: O(n). A linked list gives up the block. Each track lives in its own node, holding the value and a reference to the next node; the list itself is just a reference to the first node, the head. No index, only arrows — so reaching node i costs O(n) hops, but once you hold a node, splicing one in or out next to it is O(1): change one or two arrows, shift nothing.

Inserting Bridge after Intro: in an array, Verse, Chorus and Outro all shift one slot right; in a linked list, only two arrows are redrawn and Chorus and Outro never move

Splice a track into a 5,000-track party mix

python

A node holds a value and a next reference. Building a chain by always putting the new node in front of the head is fast, but reverses the order:

What does this print?
class Node:
    def __init__(self, val, next=None):
        self.val = val
        self.next = next

head = None
for title in ["A", "B", "C"]:
    head = Node(title, head)
print(head.val)

With no index, the only way through is walking the arrows — O(n), where a list's index was O(1):

python

Once you hold a node, inserting after it changes one or two arrows — nothing shifts. The mistake: overwriting the old arrow before the new node has taken a copy of it:

broken — fix it

This should splice Bridge in after Intro and print all four tracks. It prints Bridge over and over instead.

❓ Cross-question — "O(1)? You still had to find Intro first." True — that walk is O(n). The splice itself is O(1) only when you already hold the node, which a player usually does: it holds the node that's playing, so "play this next" needs no walk.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — the player
This piece — splice_tracks(): Inserts and removes tracks without shifting everything after them.
Scenario A listener drags Verse in after Intro and deletes Outro from a long party mix. Tunebox's playlist editor rewires two arrows for each edit instead of shifting every track after them.
splice_tracks.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
splice_tracks(tracks, edits) → listThe playlist after every splice, in order.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.