{
  "slug": "linked-list-reverse",
  "title": "Reverse a Linked List",
  "category": "Linked Lists",
  "difficulty": "medium",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Take nodes off the front of the original list one at a time and put each on the front of a new list. Because each node goes to the front, the order comes out reversed — and no extra nodes are ever created.",
  "code": [
    "prev = null; cur = head",
    "while cur != null:",
    "    next = cur.next",
    "    cur.next = prev      # flip the pointer",
    "    prev = 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": 10,
  "steps": [
    {
      "i": 0,
      "op": "lanes",
      "caption": "Reverse by moving each node from the front of the list to the front of a new one.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            7,
            3,
            9,
            4
          ],
          "focus": [
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 1,
      "op": "lanes",
      "caption": "Take 7 off the front of what remains.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            7,
            3,
            9,
            4
          ],
          "focus": [
            "read",
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 2,
      "op": "lanes",
      "caption": "Put 7 at the front of the reversed list — its pointer now aims the other way.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            7
          ],
          "focus": [
            "insert"
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            3,
            9,
            4
          ],
          "focus": [
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 3,
      "op": "lanes",
      "caption": "Take 3 off the front of what remains.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            7
          ],
          "focus": [
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            3,
            9,
            4
          ],
          "focus": [
            "read",
            null,
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 4,
      "op": "lanes",
      "caption": "Put 3 at the front of the reversed list — its pointer now aims the other way.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            3,
            7
          ],
          "focus": [
            "insert",
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            9,
            4
          ],
          "focus": [
            null,
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 5,
      "op": "lanes",
      "caption": "Take 9 off the front of what remains.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            3,
            7
          ],
          "focus": [
            null,
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            9,
            4
          ],
          "focus": [
            "read",
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 6,
      "op": "lanes",
      "caption": "Put 9 at the front of the reversed list — its pointer now aims the other way.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            9,
            3,
            7
          ],
          "focus": [
            "insert",
            null,
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            4
          ],
          "focus": [
            null
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 7,
      "op": "lanes",
      "caption": "Take 4 off the front of what remains.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            9,
            3,
            7
          ],
          "focus": [
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [
            4
          ],
          "focus": [
            "read"
          ],
          "markers": [
            {
              "name": "cur",
              "index": 0,
              "tone": "read"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 8,
      "op": "lanes",
      "caption": "Put 4 at the front of the reversed list — its pointer now aims the other way.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            4,
            9,
            3,
            7
          ],
          "focus": [
            "insert",
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 9,
      "op": "lanes",
      "caption": "Nothing left to do — the list is reversed.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "rev",
          "label": "reversed so far",
          "array": [
            4,
            9,
            3,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "head",
              "index": 0,
              "tone": "bound"
            }
          ],
          "regions": []
        },
        {
          "id": "rest",
          "label": "still to do",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    }
  ]
}