{
  "id": "linked-lists/linked-list",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/linked-lists/linked-list",
  "topic": {
    "slug": "linked-lists",
    "title": "Linked Lists"
  },
  "title": "Linked List",
  "tagline": "No shelves, no addresses — just a chain of notes saying 'next'.",
  "vizKind": "list",
  "complexity": {
    "time": {
      "best": "O(1) insert at head",
      "average": "O(n) to find",
      "worst": "O(n) to reach the tail"
    },
    "space": "O(n)",
    "growth": "linear",
    "note": "Reading the 500th item costs 500 steps, where an array costs 1. But inserting or deleting once you're there costs 1 step, where an array can cost n. Opposite strengths, exactly."
  },
  "code": {
    "javascript": "class Node {\n  constructor(value) {\n    this.value = value;\n    this.next = null;\n  }\n}\n\nfunction insertAtTail(head, value) {\n  const node = new Node(value);\n  if (head === null) return node;\n  let cur = head;\n  while (cur.next !== null) {\n    cur = cur.next;\n  }\n  cur.next = node;\n  return head;\n}\n\nfunction deleteValue(head, value) {\n  if (head === null) return null;\n  if (head.value === value) return head.next;\n  let cur = head;\n  while (cur.next !== null && cur.next.value !== value) {\n    cur = cur.next;\n  }\n  if (cur.next !== null) cur.next = cur.next.next;\n  return head;\n}",
    "python": "class Node:\n    def __init__(self, value):\n        self.value = value\n        self.next = None\n\n\n\ndef insert_at_tail(head, value):\n    node = Node(value)\n    if head is None: return node\n    cur = head\n    while cur.next is not None:\n        cur = cur.next\n\n    cur.next = node\n    return head\n\n\ndef delete_value(head, value):\n    if head is None: return None\n    if head.value == value: return head.next\n    cur = head\n    while cur.next is not None and cur.next.value != value:\n        cur = cur.next\n\n    if cur.next is not None: cur.next = cur.next.next\n    return head\n",
    "java": "class Node {\n  int value;\n  Node next;\n  Node(int value) { this.value = value; }\n}\n\n\nNode insertAtTail(Node head, int value) {\n  Node node = new Node(value);\n  if (head == null) return node;\n  Node cur = head;\n  while (cur.next != null) {\n    cur = cur.next;\n  }\n  cur.next = node;\n  return head;\n}\n\nNode deleteValue(Node head, int value) {\n  if (head == null) return null;\n  if (head.value == value) return head.next;\n  Node cur = head;\n  while (cur.next != null && cur.next.value != value) {\n    cur = cur.next;\n  }\n  if (cur.next != null) cur.next = cur.next.next;\n  return head;\n}",
    "cpp": "struct Node {\n  int value;\n  Node* next = nullptr;\n  Node(int v) : value(v) {}\n};\n\n\nNode* insertAtTail(Node* head, int value) {\n  Node* node = new Node(value);\n  if (head == nullptr) return node;\n  Node* cur = head;\n  while (cur->next != nullptr) {\n    cur = cur->next;\n  }\n  cur->next = node;\n  return head;\n}\n\nNode* deleteValue(Node* head, int value) {\n  if (head == nullptr) return nullptr;\n  if (head->value == value) return head->next;\n  Node* cur = head;\n  while (cur->next != nullptr && cur->next->value != value) {\n    cur = cur->next;\n  }\n  if (cur->next != nullptr) cur->next = cur->next->next;\n  return head;\n}"
  },
  "defaultInput": [
    4,
    8,
    15,
    16,
    23
  ],
  "defaultTarget": null,
  "inputHint": "Each value is added at the tail — watch the walk get longer every time.",
  "pitfalls": [
    {
      "wrong": "Losing the head pointer.",
      "right": "The head is the only way in. Overwrite it while walking and the entire list is unreachable — gone, with no way to get it back."
    },
    {
      "wrong": "Deleting a node by pointing at the node itself.",
      "right": "To unlink a node you need the node *before* it, because the arrows only point forwards. Keep track of the previous node as you walk."
    },
    {
      "wrong": "Forgetting to check for null before following .next.",
      "right": "The last node points at nothing. Follow that and you crash. Every walk needs a `while (cur !== null)` guard."
    },
    {
      "wrong": "Expecting a linked list to be fast because it 'has no shifting'.",
      "right": "Finding the spot is the slow part, and finding is what you do most. In practice arrays usually win — they sit together in memory, and CPUs love that."
    }
  ],
  "quiz": [
    {
      "q": "How do you reach the 100th node of a linked list?",
      "options": [
        "Jump straight to it with an index",
        "Walk from the head, 100 steps",
        "Calculate its memory address",
        "Start from the tail and walk back"
      ],
      "answer": 1,
      "why": "There are no indexes. The only way in is the head, and the only way forward is one arrow at a time."
    },
    {
      "q": "To delete a node, which node do you actually need to be holding?",
      "options": [
        "The node itself",
        "The node before it",
        "The head",
        "The node after it"
      ],
      "answer": 1,
      "why": "The arrows only point forwards. To skip past a node, you must re-aim the arrow belonging to the node *before* it."
    },
    {
      "q": "What's the linked list's real advantage over an array?",
      "options": [
        "Faster reading",
        "Uses less memory",
        "Inserting and deleting don't shuffle anything",
        "It's automatically sorted"
      ],
      "answer": 2,
      "why": "No shuffling. An array insert can move a million elements; a linked list insert re-aims one arrow. That's the entire trade you're buying."
    }
  ],
  "problems": [
    {
      "name": "Reverse Linked List",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/reverse-linked-list/"
    },
    {
      "name": "Merge Two Sorted Lists",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/merge-two-sorted-lists/"
    },
    {
      "name": "Linked List Cycle",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/linked-list-cycle/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 26,
    "frames": [
      {
        "data": {
          "nodes": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "length": 0
        },
        "explanation": "A linked list is a treasure hunt. Each node holds a value and a note saying where the next one is. There are no numbered shelves — the only way in is through the front door.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "inserting": 4,
          "length": 0
        },
        "explanation": "Make a new node holding 4. Its next pointer is empty for now.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          }
        ],
        "codeLine": 10,
        "variables": {
          "length": 1
        },
        "explanation": "The list was empty, so 4 becomes the head — the front door.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "inserting": 8,
          "length": 1
        },
        "explanation": "Make a new node holding 8. Its next pointer is empty for now.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 12,
        "variables": {
          "cur": 4,
          "steps": 1,
          "inserting": 8
        },
        "explanation": "4 has no next node — this is the tail. We walked 1 node to find it.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "active"
          }
        ],
        "codeLine": 15,
        "variables": {
          "length": 2
        },
        "explanation": "Point the old tail at 8. The chain is one link longer.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "inserting": 15,
          "length": 2
        },
        "explanation": "Make a new node holding 15. Its next pointer is empty for now.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "cur": 4,
          "steps": 1,
          "inserting": 15
        },
        "explanation": "4 points to something, so keep walking.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 12,
        "variables": {
          "cur": 8,
          "steps": 2,
          "inserting": 15
        },
        "explanation": "8 has no next node — this is the tail. We walked 2 nodes to find it.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "active"
          }
        ],
        "codeLine": 15,
        "variables": {
          "length": 3
        },
        "explanation": "Point the old tail at 15. The chain is one link longer.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "inserting": 16,
          "length": 3
        },
        "explanation": "Make a new node holding 16. Its next pointer is empty for now.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "cur": 4,
          "steps": 1,
          "inserting": 16
        },
        "explanation": "4 points to something, so keep walking.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "cur": 8,
          "steps": 2,
          "inserting": 16
        },
        "explanation": "8 points to something, so keep walking.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 12,
        "variables": {
          "cur": 15,
          "steps": 3,
          "inserting": 16
        },
        "explanation": "15 has no next node — this is the tail. We walked 3 nodes to find it.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "active"
          }
        ],
        "codeLine": 15,
        "variables": {
          "length": 4
        },
        "explanation": "Point the old tail at 16. The chain is one link longer.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 9,
        "variables": {
          "inserting": 23,
          "length": 4
        },
        "explanation": "Make a new node holding 23. Its next pointer is empty for now.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "cur": 4,
          "steps": 1,
          "inserting": 23
        },
        "explanation": "4 points to something, so keep walking.",
        "stats": {
          "comparisons": 7,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "cur": 8,
          "steps": 2,
          "inserting": 23
        },
        "explanation": "8 points to something, so keep walking.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "cur": 15,
          "steps": 3,
          "inserting": 23
        },
        "explanation": "15 points to something, so keep walking.",
        "stats": {
          "comparisons": 9,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          },
          {
            "name": "cur",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 12,
        "variables": {
          "cur": 16,
          "steps": 4,
          "inserting": 23
        },
        "explanation": "16 has no next node — this is the tail. We walked 4 nodes to find it.",
        "stats": {
          "comparisons": 10,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            },
            {
              "id": "n4-23",
              "value": 23
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "active"
          }
        ],
        "codeLine": 15,
        "variables": {
          "length": 5
        },
        "explanation": "Point the old tail at 23. The chain is one link longer.",
        "stats": {
          "comparisons": 10,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            },
            {
              "id": "n4-23",
              "value": 23
            }
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "deleting": 15
        },
        "explanation": "Now delete 15. To unlink a node you must first find the node *before* it — a linked list only points forwards.",
        "stats": {
          "comparisons": 10,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            },
            {
              "id": "n4-23",
              "value": 23
            }
          ]
        },
        "pointers": [
          {
            "name": "cur",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 23,
        "variables": {
          "cur": 4,
          "next": 8
        },
        "explanation": "The node after 4 is 8, not 15. Keep walking.",
        "stats": {
          "comparisons": 11,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n2-15",
              "value": 15
            },
            {
              "id": "n3-16",
              "value": 16
            },
            {
              "id": "n4-23",
              "value": 23
            }
          ]
        },
        "pointers": [
          {
            "name": "cur",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "found"
          }
        ],
        "codeLine": 23,
        "variables": {
          "cur": 8,
          "next": 15
        },
        "explanation": "The node after 8 is 15. Found the one before it.",
        "stats": {
          "comparisons": 12,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n3-16",
              "value": 16
            },
            {
              "id": "n4-23",
              "value": 23
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 26,
        "variables": {
          "length": 4
        },
        "explanation": "Redirect that node's arrow straight past 15. Nothing points at 15 any more, so it is gone from the list — we never had to move a single other node.",
        "stats": {
          "comparisons": 12,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "n0-4",
              "value": 4
            },
            {
              "id": "n1-8",
              "value": 8
            },
            {
              "id": "n3-16",
              "value": 16
            },
            {
              "id": "n4-23",
              "value": 23
            }
          ]
        },
        "pointers": [
          {
            "name": "head",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 27,
        "variables": {
          "length": 4,
          "walks": 12
        },
        "explanation": "Final list. Deleting was cheap once we were standing there — but *getting* there took a walk. That trade is the whole personality of a linked list.",
        "stats": {
          "comparisons": 12,
          "swaps": 0
        }
      }
    ]
  }
}