DSA — Linked Lists
What is a Linked List?
A linear data structure where elements are stored in nodes, each pointing to the next.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
Operations
| Operation | Time Complexity |
|---|---|
| Insert at head | O(1) |
| Insert at tail | O(n) |
| Delete | O(n) |
| Search | O(n) |
Insert Operations
def insert_at_head(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def insert_at_tail(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node
Delete Operation
def delete(self, data):
if not self.head:
return
if self.head.data == data:
self.head = self.head.next
return
current = self.head
while current.next:
if current.next.data == data:
current.next = current.next.next
return
current = current.next
Traversal
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
Mini Practice
- Implement a linked list
- Insert at head and tail
- Delete a node
- Traverse and display
Up Next
Continue with Doubly Linked Lists — doubly linked list.
Related Topics
Frequently Asked Questions about Linked Lists
What is Linked Lists in DSA?
Linked Lists is a fundamental concept in DSA. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Linked Lists?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Linked Lists.
Why is Linked Lists important in DSA?
Linked Lists is essential for DSA development. Understanding this concept will help you write better code and solve real-world problems more effectively.