{
  "slug": "linked-list-delete",
  "title": "Delete from a Linked List",
  "category": "Linked Lists",
  "difficulty": "easy",
  "complexity": {
    "time": "O(1) at a known node",
    "space": "O(1)"
  },
  "idea": "To delete a node, make the previous node point to the one after it. The node is unlinked and disappears. Compare with an array, where every later element has to shift left.",
  "code": [
    "walk to the node before position p",
    "prev.next = prev.next.next",
    "the unlinked node is gone"
  ],
  "example": {
    "array": [
      7,
      3,
      9,
      4
    ],
    "at": 2
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 5,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Delete the node at position 2 — value 9.",
      "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": "This is the node to unlink.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        null,
        "read",
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "prev",
          "index": 1,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "remove",
      "caption": "Point the previous node straight past 9.",
      "note": "9 leaves",
      "codeLine": 2,
      "stats": null,
      "array": [
        7,
        3,
        9,
        4
      ],
      "focus": [
        null,
        null,
        "remove",
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "prev",
          "index": 1,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "remove-close",
      "caption": "Unlinked — one pointer changed, and the chain closes up.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        7,
        3,
        4
      ],
      "focus": [
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "prev",
          "index": 1,
          "tone": "pivot"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "say",
      "caption": "Done — the chain is whole again.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        7,
        3,
        4
      ],
      "focus": [
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "head",
          "index": 0,
          "tone": "bound"
        }
      ],
      "regions": []
    }
  ]
}