Linked List Operations | Data Structures and Algorithms Day #4
Learn all about linked list operations, including insertion, deletion, traversal, and search, with examples in Python and JavaScript

Software Engineer & Technical Writer
Search for a command to run...
Learn all about linked list operations, including insertion, deletion, traversal, and search, with examples in Python and JavaScript

Software Engineer & Technical Writer
No comments yet. Be the first to comment.
In this series, I will walk you through Data Structures and Algorithms and help you prepare for coding interviews.
Learn what linked lists are, the types of linked lists (singly, doubly, and circular), and how to implement them using Python and JavaScript
affordable coding bootcamps in Kenya

Top 12 Data Structure Interview Questions Every Developer Should Know

If you're on a journey to becoming a skilled web developer or full-stack engineer, finding the right resources is essential. GitHub is a goldmine for such resources, with repositories that cover everything from basic HTML and CSS to advanced topics l...

Whether you're starting out in software engineering or looking to refine your skills, GitHub is a treasure trove of high-quality resources. Here’s a list of 9 GitHub repositories that can help you grow your technical knowledge, deepen your problem-so...

Python has a vast ecosystem of command-line tools, but installing these globally with pip can lead to version conflicts and dependency issues. pipx offers a modern solution by enabling global installations in isolated environments. In this guide, we'...

In the world of programming, linked lists are fundamental data structures used to organize and store data dynamically. Understanding linked list operations—such as insertion, deletion, traversal, and search—is crucial for mastering algorithms and efficient data manipulation. Linked lists offer flexibility in memory management, making them preferable to arrays in certain scenarios. This article breaks down key linked list operations, complete with Python and JavaScript examples, along with an analysis of their time complexities.
Operations on linked lists allow us to manipulate nodes (elements) and modify the data structure efficiently. Mastery of these operations enables developers to build robust solutions involving stacks, queues, and even complex graph algorithms.
Inserting a new node can happen at the beginning, end, or a specific position in the list.
Python Example:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# Insert at the beginning
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
JavaScript Example:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
// Insert at the beginning
insertAtBeginning(data) {
const newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
}
At the Beginning: O(1)
At the End: O(n)
At a Specific Position: O(n)
We can delete a node from the beginning, end, or specific position in the linked list.
Python Example:
def delete_from_beginning(self):
if self.head is None:
return
self.head = self.head.next
JavaScript Example:
deleteFromBeginning() {
if (this.head !== null) {
this.head = this.head.next;
}
}
At the Beginning: O(1)
At the End: O(n)
At a Specific Position: O(n)
Traversal involves visiting each node in the list from the head to the end.
Python Example:
def traverse(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
JavaScript Example:
traverse() {
let current = this.head;
while (current) {
console.log(current.data + " -> ");
current = current.next;
}
}
n nodes.Searching involves checking if a specific value exists in the list.
Python Example:
def search(self, value):
current = self.head
while current:
if current.data == value:
return True
current = current.next
return False
JavaScript Example:
search(value) {
let current = this.head;
while (current) {
if (current.data === value) return true;
current = current.next;
}
return false;
}
| Operation | Singly Linked List | Doubly Linked List |
| Insertion at Head | O(1) | O(1) |
| Insertion at Tail | O(n) | O(1) |
| Deletion at Head | O(1) | O(1) |
| Deletion at Tail | O(n) | O(1) |
| Search | O(n) | O(n) |
"Linked list insertion is always fast."
"Doubly linked lists are always better."
To insert a node, create a new node and adjust the pointers to maintain the structure. Refer to the examples above for Python and JavaScript implementations.
The time complexity for traversing a linked list is O(n), where n is the number of nodes.
Arrays offer faster access with O(1) indexing, but linked lists excel in dynamic memory management, making them ideal for certain algorithms and data storage needs.
Mastering linked list operations—such as insertion, deletion, traversal, and search—is key to solving more complex data structure problems. Now that you understand the basic operations, try implementing them in your own code. Experiment with both singly and doubly linked lists to see which one suits your needs better.
If you have any questions or challenges, feel free to drop a comment below or explore our related articles for more insights!