Algorithmsconcept mapping

Linked List Concept Mapping

TT
Testlaa Team
May 14, 20261 min read

Map real objects to nodes: train cars, playlist songs, undo steps. The invariant is: follow next until None.

Try this in Python

from __future__ import annotations
from dataclasses import dataclass


@dataclass
class Node:
    val: int
    next: Node | None = None


def to_list(head: Node | None) -> list[int]:
    out: list[int] = []
    while head:
        out.append(head.val)
        head = head.next
    return out


def from_list(vals: list[int]) -> Node | None:
    dummy = Node(0)
    cur = dummy
    for x in vals:
        cur.next = Node(x)
        cur = cur.next
    return dummy.next

# Map index -> walk steps
def get_at(head: Node | None, i: int) -> int | None:
    while head and i:
        head = head.next
        i -= 1
    return head.val if head else None

print(get_at(from_list([10, 20, 30]), 1))

Key takeaways

  • Index access is O(n)—unlike arrays.
  • Loop with a counter is the basic walk.
  • For interviews, state whether you mutate or read-only.

Tags:

Linked listsPythonStudents