Arrays are fast and simple, so why invent anything else? Because arrays have one big weakness: their elements must sit side by side. Whenever you add or remove something anywhere except the end, the neighbours have to move. Linked lists exist to remove that cost.
Say you have [10, 20, 30, 40] and want to put 5 at the front. In an array there is no empty slot at index 0 — so every element has to move one place to the right first. Four elements means four moves; a million elements means a million moves.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, 2 dry-run simulators and code in JavaScript, Python, Java and C++.
Still to come