{
  "slug": "linked-list-traverse",
  "title": "Traverse a Linked List",
  "category": "Linked Lists",
  "difficulty": "intro",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "A linked list is a chain of nodes; each node holds a value and a pointer to the next one. You cannot jump to index 5 — you must start at the head and follow pointers until you reach ∅.",
  "code": [
    "cur = head",
    "while cur != null:",
    "    visit cur.value",
    "    cur = cur.next"
  ],
  "example": {
    "array": [
      7,
      3,
      9,
      4
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 6,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "A list of 4 nodes. Each points to the next; the last points to ∅.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 1,
      "op": "read",
      "caption": "Visit node 0 — value 7. Then follow its next pointer.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        "read",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "cur",
          "index": 0,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "read",
      "caption": "Visit node 1 — value 3. Then follow its next pointer.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        "read",
        null,
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "cur",
          "index": 1,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "read",
      "caption": "Visit node 2 — value 9. Then follow its next pointer.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        null,
        "read",
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "cur",
          "index": 2,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "read",
      "caption": "Visit node 3 — value 4. Then follow its next pointer.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        null,
        null,
        "read"
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "cur",
          "index": 3,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "say",
      "caption": "The next pointer is ∅ — we have reached the end of the list.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        }
      ],
      "regions": []
    }
  ]
}