DSA
    beginner

    Linked List

    Software Engineer path · lesson 3 of 99
    O(1) insert at head

    Syntax: inserthead n · inserttail n · search n · delete n · reverse. Numbers from -99 to 99.

    Try:

    head → null

    curprevFoundPointer changedRe-linkedBeing removedReversed so far

    Empty list: head is null.

    Code (running line highlighted)
    1. 1insertHead(x): n = Node(x); n.next = head; head = n # O(1)
    2. 2insertTail(x): walk to the last node; last.next = Node(x) # O(n)
    3. 3search(x): cur = head; while cur and cur.val != x: cur = cur.next
    4. 4delete(x): find prev of x; prev.next = x.next # unlink
    5. 5reverse(): prev = None; while cur: nxt = cur.next; cur.next = prev; prev, cur = cur, nxt
    Pointer hops
    0 / 6
    Pointer writes
    0 / 6
    Invariant: Following next from head reaches every node exactly once, ending at null.

    Tap any element in the diagram to inspect its state at this step.

    1 / 15

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    Reverse a singly linked list

    Sequence

    Order one iteration of the three-pointer reversal, from set-up to finish.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the time complexity to access the Nth element?

    The short version

    Think of a treasure hunt where each clue tells you exactly where to find the next clue. You can't skip ahead; you must follow the chain. Unlike a bookshelf (Array) where you can grab the 5th book instantly.

    In more depth

    Dynamic Chains

    A Linked List is a linear data structure where elements are not stored in contiguous memory locations. Instead, each element (node) points to the next.

    Pros vs Arrays:

    Dynamic Size: Can grow or shrink indefinitely (until memory runs out).
    Efficient Insert/Delete: Inserting at the beginning or middle (if you have the pointer) is O(1), unlike arrays which require shifting elements O(n).

    Cons:

    No Random Access: You cannot jump to index i. You must traverse from the head.
    Extra Memory: Overhead for storing pointers.

    Tortoise and Hare:

    A classic technique for Linked Lists is using two pointers moving at different speeds to detect cycles or find the middle element.

    When to reach for this

    • When size is unknown or changes frequently
    • When you need constant time Insert/Delete at the start
    • Implementing Stacks or Queues

    Interview Questions

    Amazon
    Google
    Netflix
    Microsoft
    Apple
    LinkedIn
    Amazon
    Uber
    Bloomberg
    Google
    Facebook
    Airbnb

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required