linkedList.definition
linkedList.descriptionPart1
linkedList.descriptionPart2
linkedList.keyPointsTitle
- linkedList.keyPoint1
- linkedList.keyPoint2
- linkedList.keyPoint3
- linkedList.keyPoint4
linkedList.exampleTitle
linkedList.syntaxTitle
// Java
class Node {
int value;
Node next;
Node(int value) {
this.value = value;
}
}
class LinkedListExample {
Node head;
// Insert at end
void insert(int value) {
Node newNode = new Node(value);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}# Python
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, value):
node = Node(value)
if not self.head:
self.head = node
return
current = self.head
while current.next:
current = current.next
current.next = node// C++
#include <iostream>
struct Node {
int value;
Node* next;
Node(int v) : value(v), next(nullptr) {}
};
class LinkedList {
public:
Node* head = nullptr;
void insert(int value) {
Node* node = new Node(value);
if (!head) {
head = node;
return;
}
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = node;
}
};// JavaScript
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
insert(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
}Interactive Linked List Visualization
List is empty. Insert a value to begin.
Note: Insert appends to end (O(n) for traversal, O(1) for actual insert). Remove/Search traverse from head until found.
enhanced.realWorldExamples.title
Music apps use doubly linked lists for next/previous song navigation.
Why? Efficient insertion/deletion without shifting elements.
OS uses linked lists to track free memory blocks.
Why? Dynamic allocation without contiguous memory requirement.
Time Complexity
Singly linked lists allow constant-time operations at the head but require O(n) traversal for arbitrary inserts, removals, and searches.
| Operation | Time | Why? |
|---|---|---|
| Create (n) | O(n) | Allocate n nodes and link them |
| Insert (anywhere) | O(n) | Traverse to target index then link new node |
| Remove (anywhere) | O(n) | Traverse to previous node and unlink target |
| Search | O(n) | Traverse nodes one by one |
| Access HEAD/Tail | O(1) | Head pointer is always available |
| Clear | O(n) | Visit all nodes and reset links |
enhanced.complexityAnalysis.title
enhanced.complexityAnalysis.mathematicalTitle
Access requires traversal from head, visiting each node sequentially. For element at position k, we must traverse k nodes, giving O(k) which is O(n) in worst case.
๐ขBest Case
O(1) for insertion/deletion at head. O(1) for access if element is at head.
๐กAverage Case
O(n/2) = O(n) for random access and search operations.
๐ดWorst Case
O(n) for access, search, and insertion/deletion at arbitrary positions.
๐พSpace Complexity
O(n) with additional pointer overhead per node (typically 8 bytes for next pointer on 64-bit systems).
โกCache Performance
Poor cache locality as nodes may be scattered in memory. Each access may cause a cache miss.
enhanced.codeWalkthrough.title
enhanced.codeWalkthrough.stepByStep
enhanced.practiceProblems.title
Reverse Linked List
easyReverse a singly linked list and return the new head.
1 -> 2 -> 3 -> 4 -> 55 -> 4 -> 3 -> 2 -> 1Continue Learning
Practice operations and explore implementations for singly linked lists.