Quick Answer
Linked list data structure has 4 core operations. They are traverse, add at front, add at end and delete. Every node stores a value and a pointer to the next node. Adding at the front costs O(1) and reaching a position costs O(n). Choose it for frequent inserts at the front. Choose an array for direct access by position. Python and C++ code below was executed.
Linked list data structure means a chain of nodes. Every node stores one value and the address of the next node.
Think of a treasure hunt. Every clue names one item and tells you where the next clue sits. You follow the chain one step at a time.
That design makes a linked list strong where an array is weak and weak where an array is strong. Code, measured timings and a clear rule for choosing follow below.
What Is a Linked List Data Structure?
Every node has two parts. One part stores a value like a number or a name. Second part stores a pointer to the next node.
Lists have one extra pointer called the head. It points to the first node and the last node points to nothing, which marks the end.
Arrays store all values together in one memory region. Nodes of a singly linked list sit anywhere in memory and connect only through pointers.
How Does a Singly Linked List Work Step by Step?
Say this list has three nodes with values 10, 20 and 30. Its head points to 10 and 10 points to 20. Node 20 points to 30 and node 30 points to nothing.
Traversal starts at the head and follows every pointer. You visit 10 then 20 then 30. You cannot jump straight to the third node.
Now add 5 at the front. Create a node for 5 and point it at the old first node before you move the head to the new node. Three steps finish the insert and no other node moves.
Watch the pointers move on the linked list lesson if you want to see the steps animated.
Which 4 Operations Does a Linked List Data Structure Need?
Table below lists the 4 operations with their time cost. Costs counts steps in the worst case.
| Operation | Time | Why |
|---|---|---|
| Operation #1 Traverse | O(n) | Visits every node once |
| Operation #2 Add at front | O(1) | Updates two pointers only |
| Operation #3 Add at end | O(n) | Walks to the last node first |
| Operation #4 Delete a value | O(n) | Searches before it relinks |
Operation #1: Traverse (Visit Every Node)
What you get. Printing and searching both start here. Start at the head and move to next until the pointer is empty.
Operation #2: Add at Front (Cheapest Insert)
What you get. Inserts cost the same for 10 nodes or 10 million. New node points at the old head and then becomes the head.
Operation #3: Add at End (Walk to the Tail)
What you get. Appends need one walk through the list. Storing a tail pointer beside the head makes repeated appends cost O(1).
Operation #4: Delete a Value (Relink Around It)
What you get. Removals move no other node. Find the node before the removed value and point it at the node after that value.
Deleting the first node is a special case because no earlier node exists. Move the head to the second node instead.
How Do You Code Singly Linked List Operations in Python and C++?
Python version below has all 4 operations. Method to_list returns the traversal as a list so you can check the result.
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add_front(self, value):
node = Node(value)
node.next = self.head
self.head = node
def add_end(self, value):
node = Node(value)
if self.head is None:
self.head = node
return
current = self.head
while current.next is not None:
current = current.next
current.next = node
def delete(self, value):
if self.head is None:
return False
if self.head.value == value:
self.head = self.head.next
return True
current = self.head
while current.next is not None:
if current.next.value == value:
current.next = current.next.next
return True
current = current.next
return False
def to_list(self):
items = []
current = self.head
while current is not None:
items.append(current.value)
current = current.next
return items
C++ needs manual memory care. Function removeValue calls delete on the removed node so memory is freed.
#include <iostream>
using namespace std;
struct Node {
int value;
Node* next;
};
Node* addFront(Node* head, int value) {
Node* node = new Node{value, head};
return node;
}
Node* addEnd(Node* head, int value) {
Node* node = new Node{value, nullptr};
if (head == nullptr) return node;
Node* current = head;
while (current->next != nullptr) current = current->next;
current->next = node;
return head;
}
Node* removeValue(Node* head, int value) {
if (head == nullptr) return nullptr;
if (head->value == value) {
Node* rest = head->next;
delete head;
return rest;
}
Node* current = head;
while (current->next != nullptr && current->next->value != value) current = current->next;
if (current->next != nullptr) {
Node* gone = current->next;
current->next = gone->next;
delete gone;
}
return head;
}
void traverse(Node* head) {
for (Node* current = head; current != nullptr; current = current->next)
cout << current->value << " ";
cout << endl;
}
int main() {
Node* head = nullptr;
head = addEnd(head, 20);
head = addEnd(head, 30);
head = addFront(head, 10);
head = removeValue(head, 20);
traverse(head);
return 0;
}
Tested. Python 3.13.18 passed checks for add at front, add at end, delete of first, middle and last nodes and an empty list. g++ 13.3 compiled the C++ sample with warnings on and printed 10 30. Both ran in a cloud sandbox.
What Is the Time Complexity of a Linked List?
Add at front is O(1) and reaching a node by position is O(n). Space is O(n) with one extra pointer per node.
Python documentation states that inserts and pops at the beginning of a list are slow because all other elements shift by one. Its data structures tutorial names collections.deque for efficient appends and pops at both ends.
Timings below show the gap. Test inserted 50,000 values at the front using the best of 5 trials in CPython 3.13.18.
| Structure | Time for 50,000 front inserts |
|---|---|
Python list insert(0, x) | 0.2103 s |
Linked list add_front | 0.0094 s |
collections.deque appendleft | 0.0011 s |
Heads Up. Reaching the node at position 25,000 took 461 microseconds in the same test while the array index took 0.075 microseconds. Linked lists win at the front and lose at random positions. Your numbers will differ by machine.
Should You Choose a Linked List or an Array?
Choose between a linked list data structure and an array by the operation you repeat most. Table below compares the two on the costs that matter.
| Task | Linked list | Array |
|---|---|---|
| Access by position | O(n) | O(1) |
| Insert at front | O(1) | O(n) |
| Insert after a known node | O(1) | O(n) |
| Memory layout | Scattered nodes | One region |
Use a linked list for many front inserts and few position lookups. Choose an array for lookups by position. Both stack and queue add and remove at the ends so a linked list suits them.
Big O explains the table. Open time complexity and Big O notation for the full picture.
What Mistakes Break a Linked List Data Structure?
Table below lists 4 bugs that appear in beginner code and the fix for the bug.
| Mistake | What happens | Fix |
|---|---|---|
| Lose the head pointer | Every node becomes unreachable | Store the head before any edit |
| Ignore the empty list case | Code crashes on a null head | Test the head first |
| Forget to move the head on delete | Deleted node remains in the list | Reassign head when the first node goes |
| Never call delete in C++ | Removed nodes leak memory | Free the node after relinking |
Frequently Asked Questions
Do Nodes Store the Values of a Linked List?
Yes. Nodes store values and pointers join the nodes into a chain.
Is Insert at the Front Always O(1)?
Yes. Head moves to the new node after it points at the old head. Two pointer updates finish it.
Is Access by Position O(1) in a Linked List?
No. You must follow pointers from the head so reaching position k costs O(k) steps.
Is a Linked List Faster Than an Array?
No, not always. Linked lists win at front inserts and arrays win at access by position.
Does Python Have a Built In Linked List?
No. Python has no singly linked list type. Its documentation recommends collections.deque for efficient appends and pops at both ends.
Do You Need the Head Pointer?
Yes. Losing the head loses every node because it is your only way into the list.
Can a Linked List Store Different Data Types?
Yes in Python because a node can store any object. C++ needs one declared type or a template.
What Should You Remember About the Linked List Data Structure?
Pointers give a linked list quick front inserts and slow position lookups. Choose it for the operation you repeat most.
Key takeaways.
- Every node stores a value and a pointer to the next node.
- Add at front costs O(1) and access by position costs O(n).
- Handle the empty list and the first node in every delete.
Practice with the linked list practice problems and check what you learned.