Back to All Algorithms
Singly Linked List
Category: Data Structures | Time: O(n) Search | Space: O(n)
Visualization
Visual representation of the data structure
Enter commands to build the linked list.
About Singly Linked List

A linear data structure where elements are stored in nodes. Each node contains a data field and a reference (or 'pointer') to the next node in the sequence. Unlike arrays, linked lists do not have a fixed size and allow for efficient insertions and deletions.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Variable State
{
  "status": "Initializing Linked List"
}
Singly Linked List: How It Works
Last updated October 28, 2026

Overview

A singly linked list is a chain of nodes, each holding a value and a pointer to the next node. The list keeps a reference to the first node, the head, and the last node points to null. Unlike an array, the nodes do not need to be next to each other in memory.

That layout makes inserting or removing a node O(1) once you have a reference to its predecessor — you only change a pointer — but reaching the k-th element requires walking k links from the head. There is no random access.

The default commands insert 10, 20 and 5 at the end, delete 20 (by pointing 10's next directly at 5), then insert 15. The final list is 10 -> 5 -> 15.

Step by step

  1. Insert at the end: walk to the last node (or keep a tail pointer) and set its next to the new node.
  2. Insert at the head: set the new node's next to the current head, then make it the head.
  3. Delete a value: walk with a prev pointer until current holds the value, then set prev.next = current.next.
  4. Traverse: start at the head and follow next until null.

Complexity

CaseCostWhy
Access / searchO(n)Must walk from the head.
Insert / delete at headO(1)Only the head pointer changes.
Insert at tailO(n) or O(1)O(1) if a tail pointer is maintained.
Delete by valueO(n)Finding the node dominates; unlinking is O(1).

When to use it

  • Frequent insertions and deletions at the front, such as a simple stack.
  • Building blocks for hash table chains, adjacency lists and free lists in memory allocators.
  • Interview classics: reversing a list, detecting cycles, merging sorted lists, finding the middle.

Common mistakes

  • Deleting the head is a special case; a dummy (sentinel) head node removes it.
  • Losing the rest of the list by overwriting next before saving it, especially when reversing.
  • Forgetting to update the tail pointer when the last node is deleted.
Worked Example: Singly Linked List Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Singly Linked List: follow each row, predict the next one, and compare. The run below takes 13 steps in total. The Line column refers to the line numbers in the code listing above.

Input
insert 10,insert 20,insert 5,delete 20,insert 15
Result
status = Operations complete, final_list = [10 -> 5 -> 15]
StepLineWhat happens
17status = Initializing Linked List
211action = insert(10)
319status = Inserted 10, head = 10
411action = insert(20)
519status = Inserted 20, head = 10
611action = insert(5)
719status = Inserted 5, head = 10
811action = delete(20)
911action = delete(20), status = Found 20 at index 1
1011status = Deleted 20, head = 10
1111action = insert(15)
1219status = Inserted 15, head = 10
1320status = Operations complete, final_list = [10 -> 5 -> 15]
Frequently Asked Questions
About Singly Linked List

What is a Linked List?

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.

What is the difference between an Array and a Linked List?

Arrays are fixed-size and store elements in contiguous memory, allowing for O(1) random access. Linked lists are dynamic in size and use pointers to connect nodes, which means access/search is O(n), but insertions and deletions are more efficient at O(1) if the node's location is known.

What are the primary operations of a Linked List?

  • Insertion: Adds an element at the beginning, end, or a specific position.
  • Deletion: Removes an element from the list.
  • Search: Finds an element in the list.
  • Traversal: Iterates through all the nodes in the list.

What is a 'node' in a linked list?

A node is the basic building block of a linked list. It typically consists of two parts: the data itself, and a pointer (or link) that references the next node in the sequence.

What is the 'head' of a linked list?

The 'head' is a pointer that stores the address of the very first node in the linked list. If the head is null, the list is empty. It's the entry point to the entire list.

What is the time complexity for operations?

  • Access/Search: O(n) because you may have to traverse the entire list.
  • Insertion/Deletion at beginning: O(1).
  • Insertion/Deletion at end: O(n) because you must traverse to the end first.

What is the space complexity of a Linked List?

The space complexity is O(n) because it stores n elements, and each element's node requires space for the data and a pointer.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.