What is a Linked List?
Data Structures·7 min·Python code
Linked lists chain nodes, and every node stores a value and a pointer onward. That layout lets you insert or remove a node in O(1) time once you hold it, without shifting other elements.
Nodes Chained by Pointers
Picture a scavenger hunt. Every clue holds information and directions to the next clue, and the structure works the same way. Every node holds a value and a pointer to the next node.
You keep one reference to the first node, called the head, and follow pointers one hop at a time until a next pointer is null, which marks the end.
Nodes strike a different bargain from an array. Arrays store elements side by side, so they jump to index 5 instantly, but inserting near the front shifts everything after it.
Index access is the price, since reaching the fifth node means walking from the head. In exchange you get cheap insertion and deletion, because you only rewire a couple of pointers once you hold the right node.
Singly Linked Lists vs Doubly Linked Lists
Singly linked lists give every node one pointer, to its successor, so you travel forward and never back. That compact layout suits stacks, queues and hash table buckets.
Doubly linked lists add a second pointer, prev, so every node knows its successor and its predecessor. You traverse in either direction and delete a given node in O(1) without walking to find the node before it.
That is why an LRU cache pairs two way links with a hash map. Cost is one extra pointer per node, plus more bookkeeping on every update.
Circular lists link the tail back to the head instead of pointing to null.
Reversing a Linked List and Other Pointer Patterns
Most problems here are pointer rewiring puzzles, and three patterns cover the majority. Reversing a list uses three pointers, prev, curr and a saved next, and flips curr.next backward on every step.
Save the next node before you overwrite the pointer, or you lose the rest of the list.
Cycle detection uses the two pointers technique known as Floyd's tortoise and hare. Advance one pointer one node and another two nodes, and if they meet there is a loop. If the second pointer reaches null, there is none.
Merging two sorted lists splices existing nodes together behind a dummy head and always attaches the smaller front value. None of these patterns move data. They only redirect pointers.
Practise them on CodeStory's linked list practice problems.
Linked List Code Example in Python
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
def reverse(head):
prev = None
curr = head
while curr:
nxt = curr.next # save the next node first
curr.next = prev # flip the pointer backward
prev = curr # advance prev
curr = nxt # advance curr
return prev # prev is the new head
Edit this code in the browser IDE on CodeStory, free on the platform. Open the CodeStory App
Linked List Time and Space Complexity
| Time | O(1) to insert or delete at a known node. O(n) to search or reach the k-th element because you follow pointers from the head |
|---|---|
| Space | O(n) for n nodes. Iterative traversal and reversal need O(1) extra space, and recursive versions need O(n) stack |
Search costs O(n) in the average and worst case, and O(1) only when the target is the head. Binary search does not work on these lists because they lack index access.
When to Use a Linked List
- You insert or delete often at the front or middle, you already hold a pointer to that spot and you do not need index access.
- You build a stack, queue or hash table buckets, where the collection grows and shrinks constantly.
- You build an LRU cache, where doubly linked lists give O(1) move to front and eviction beside a hash map.
- You want to avoid contiguous memory or occasional resize copies, like a memory allocator's free list.
Mistakes to Avoid
- Overwriting a next pointer before saving it, which orphans the rest of the list. Store the next node in a temporary variable first.
- Mishandling head and tail cases, like inserting into an empty list or deleting the head. Dummy nodes remove most special cases.
- Updating only one direction with two way links. Every insert or delete must fix both next and prev pointers of the neighbours.
Linked List FAQs
Is a linked list hard to learn?
Concept is short, nodes chained by pointers, but one wrong assignment can lose half the list. Draw the nodes and arrows, or follow the pointer moves step by step, and it clicks.
When should I use a linked list instead of an array?
Use nodes when you insert or delete often at positions you already hold and do not need index access. Use an array when you need random access or cache friendly iteration, since arrays store elements contiguously and perform better in practice.
What is the time complexity of searching a linked list?
O(n), because you follow next pointers from the head until you find the value or reach the end. Binary search fails here, since you cannot jump to a middle element in constant time.
How do you detect a cycle in a linked list?
Use Floyd's tortoise and hare. Move one pointer one node at a time and another two nodes at a time. If they meet, the list has a cycle, and if the second pointer reaches null, it does not. Cost stays O(n) time and O(1) extra space.
What is the difference between a singly and a doubly linked list?
Singly linked nodes store one pointer, pointing forward. Doubly linked nodes store a second pointer to the previous node, which allows two way traversal and O(1) deletion of a known node at the cost of extra memory.
How do you reverse a linked list?
Keep prev, curr and a saved next, then flip curr.next to point at prev and advance both pointers until curr is null. Reversal costs O(n) time and O(1) space.
How do I practise pointers on CodeStory?
Work through CodeStory's linked list practice problems and draw every pointer change before you code it.
Related Guides
Guides are step one. In a CodeStory batch you rewire the nodes yourself, then defend every pointer change in a scored mock round.
Talk to a Mentor