DSA — Linear DS
Singly Linked List
Nodes connected by pointers. Efficient O(1) head insertion/deletion but no random access. Foundation for stacks, queues, and more.
O(n)Access
O(1)Insert Head
O(n)Search
Node→Traversal
✓Dynamic
03
Interactive List
Modify the list to see O(1) vs O(n) operations.
How It Works
Code
Complexity
Quiz
Practice
01
Node Structure
A node contains its value and a pointer (
next) to the following node.02
Insert Head
Simply point the new node's next to current head. Constant time O(1).
03
Traversal
Start from head and follow next pointers until null. Linear time O(n).
04
Delete Node
Update previous node's next to skip the deleted node. O(n) to find, O(1) to delete.
Python — SLL Implementation
class Node: def __init__(self, val): self.val = val self.next = None class SLL: def __init__(self): self.head = None def push_front(self, val): # O(1) node = Node(val) node.next = self.head self.head = node def append(self, val): # O(n) node = Node(val) if not self.head: self.head = node return curr = self.head while curr.next: curr = curr.next curr.next = node def pop_front(self): # O(1) if not self.head: return None val = self.head.val self.head = self.head.next return val def find(self, val): # O(n) curr = self.head while curr: if curr.val == val: return True curr = curr.next return False
| Operation | Time | Space |
|---|---|---|
| Access | O(n) | O(1) |
| Insert Head | O(1) | O(1) |
| Insert Tail | O(n) | O(1) |
| Delete Head | O(1) | O(1) |
| Delete Tail | O(n) | O(1) |
| Search | O(n) | O(1) |
Tip: Keep a tail pointer to make append O(1). This is commonly done in practice for queues.
1. Why is inserting at the head O(1) while inserting at the tail is O(n)?
2. Reading the 500th element of a singly linked list costs:
3. To delete a node you hold a pointer to, what else do you normally need?
4. The fast/slow pointer technique advances one pointer two steps and the other one. What does that find in a single pass?
5. Reversing a singly linked list iteratively means tracking how many pointers?
Progress0 / 8 solved