-
Hajipur, Bihar, 844101
A linked list is one of the most important data structures you will study when learning DSA. It works differently from arrays because the elements are not stored next to each other in memory. Instead, each element stores its own value and a link to the next element. This creates a flexible chain of nodes that can grow and shrink without moving other elements. Linked lists are used in operating systems, browsers, media players, queues, stacks and situations where dynamic memory usage is important.
In this tutorial, you will learn what linked lists are, how nodes work, how pointers create the connections, how to build your own linked list class in Python and how the basic operations like inserting, deleting and traversing work. You will also see real-life uses and several practice examples that show how linked lists help you manage ordered data efficiently.
A linked list is a sequence of nodes connected through pointers. Each node has two parts:
A value
A reference to the next node
The last node points to None, which marks the end of the list.
Unlike arrays, you do not need to declare a fixed size. Linked lists grow as you add nodes.
A node represents one element of the linked list.
You can imagine nodes as boxes arranged in a line. Each box stores:
The actual data
A link to the next box
Example visual layout:
[10 | next] → [20 | next] → [30 | next] → None
This gives linked lists their dynamic nature.
Linked lists are useful when you need efficient insertions and deletions. In arrays, adding or removing an element requires shifting many items. In a linked list, you simply change the pointers.
Use cases include:
Music playlist navigation
Undo/redo features
Browser history
Memory management in operating systems
Queues and stacks
These situations rely heavily on linked list behavior.
In Python, you begin by defining a simple Node class.
class Node:
def __init__(self, data):
self.data = data
self.next = None
This class holds the value and the pointer.
Next, you build your LinkedList class to manage nodes.
class LinkedList:
def __init__(self):
self.head = None
The head is the first node in the list. If the list is empty, head is None.
Adding at the beginning is simple and fast.
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
This operation does not require shifting elements, which makes it efficient.
To insert at the end, you first walk through the list until you reach the last node.
def insert_at_end(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
This adds the new node as the last element.
Traversal is the process of walking through all nodes and reading their values.
def traverse(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
Traversal is useful for displaying or searching.
Deleting a node requires updating the previous node’s pointer.
def delete_beginning(self):
if self.head:
self.head = self.head.next
def delete_value(self, key):
temp = self.head
if temp and temp.data == key:
self.head = temp.next
return
prev = None
while temp and temp.data != key:
prev = temp
temp = temp.next
if temp:
prev.next = temp.next
This removes the node containing the given value.
Searching means checking if a value exists in the list.
def search(self, key):
temp = self.head
while temp:
if temp.data == key:
return True
temp = temp.next
return False
Linked lists do not support direct index access, so searching is linear.
Songs are linked so the player can move next or previous without shifting anything.
Pages are linked like a chain, allowing forward and backward movement.
Operating systems use linked lists to track free memory blocks.
Behind the scenes, linked lists support stack and queue operations efficiently.
Images are linked so you can move to the next or previous photo smoothly.
These examples highlight how often linked lists appear in daily-use programs.
history = LinkedList()
history.insert_at_end("Home")
history.insert_at_end("Products")
history.insert_at_end("Cart")
history.traverse()
This describes how pages are connected in order.
playlist = LinkedList()
playlist.insert_at_end("Song 1")
playlist.insert_at_end("Song 2")
playlist.insert_at_end("Song 3")
playlist.traverse()
This shows how songs play one after another.
ll = LinkedList()
ll.insert_at_beginning(10)
ll.insert_at_end(20)
ll.traverse()
ll.delete_beginning()
ll.delete_value(30)
ll.search(20)
ll.head is None
ll.insert_at_end(50)
ll.insert_at_end(60)
ll.delete_value(50)
ll = LinkedList()
ll.insert_at_end(5)
ll.insert_at_end(10)
ll.insert_at_end(15)
ll.traverse()
A linked list is a dynamic structure made of nodes that store values and links. You learned how nodes connect through pointers, how to create and manage a linked list in Python, how to insert and delete nodes, and how traversal and searching work. Linked lists support many real applications like browser history, playlist navigation, memory handling, queues and stacks. Once you understand how linked lists function, you can use them confidently in more advanced structures like trees, graphs and hash tables.
Q1. Write a Python program to create a linked list with 4 nodes having values: 10, 20, 30, and 40.
Q2. Write a Python program to define a function that displays all values in the linked list.
Q3. Write a Python program to insert a new node at the beginning of the linked list.
Q4. Write a Python program to insert a new node at the end of the linked list.
Q5. Write a Python program to delete a node with value 20 from the linked list.
Q6. Write a Python program to count the total number of nodes in the linked list.
Q7. Write a Python program to check if a specific value exists in the linked list.
Q8. Write a Python program to reverse the linked list.
Q9. Write a Python program to create a function that searches a node by value and returns its position.
Q10. Write a Python program to print the value of the last node in the linked list.