Linked list
Связный список
A linked list stores items in nodes that point to one another. Each node holds a value and a link to the next node; in a doubly linked list, it also links to the previous one.
A sequence made of links
An array lets you jump straight to an index. A linked list gives you a starting point instead: follow the links to reach the item you want. Each node holds a value and at least one link, usually a reference or pointer. The nodes do not need to sit next to each other in memory.
The list keeps a reference to its first node, called the head, and may also keep a tail reference to the last node. A singly linked list has only a next link per node; a doubly linked list adds a prev link so you can move in either direction. In a non-circular list, the last node’s next is null. In a circular list, it points back to the head.
Finding a node versus changing a link
Getting an item by index or searching for a value takes O(n) time in the worst case because you may have to walk through the list. Changing the links is a separate operation. Prepending a node or inserting it after a node you already have takes O(1). Appending also takes O(1) if the list tracks its tail; without that reference, you normally have to find the last node first.
For deletion, the kind of list matters. In a singly linked list, removing an interior node requires updating the link from its predecessor. Having a reference to the node you want to remove does not tell you where its predecessor is. In a doubly linked list, the node gives you access to both neighbors, so removal takes O(1) once you have that node.
Example: insert without losing the rest of the list
Suppose a is an existing node and new_node is a fresh node. To insert the new node immediately after a in a singly linked list:
new_node.next = a.next
a.next = new_node
The first line preserves the link to the rest of the list before the second line replaces it. Reversing the lines would make the new node point to itself. If a was the tail and the list tracks its tail, update that reference as well.
When a linked list makes sense
A singly linked list with head and tail references can implement a queue with constant-time insertion at the tail and removal from the head. An LRU cache often pairs a hash table with a doubly linked list: the table finds an entry by key, and the list lets the cache move that entry to the most-recently-used end without a scan.
Constant-time insertion alone is not a reason to choose a linked list. If the caller gives you an index rather than a node reference, locating the insertion point can dominate the cost. Even a full scan, O(n) for both lists and arrays, is often faster on an array because reading adjacent memory makes better use of the CPU cache.
What interviewers look for
Common exercises include reversing a list in place, finding its middle, and detecting a cycle. Explain which references you have before stating a time bound. Then test an empty list, a single-node list, and any case where the head or tail changes.