Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program To Add Elements To A Linked List
A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a reference to the next node. Unlike arrays, linked list elements are not stored in contiguous memory locations, making insertion and deletion operations more efficient.
In Python, we can implement a linked list using classes. Each node contains data and a pointer to the next node. The first node is called the head, and the last node points to None.
Linked List Structure
Every node in a linked list contains ?
Data The actual value stored in the node
Next A reference/pointer to the next node
There are three main ways to add elements to a linked list: at the beginning, at the end, or at a specific position in the middle.
Adding Elements at the Beginning
When adding at the beginning, the new node becomes the new head of the linked list ?
Algorithm
Step 1 Create a new node with the given data
Step 2 Point the new node to the current head
Step 3 Update head to point to the new node
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def display(self):
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements
# Example usage
ll = LinkedList()
ll.insert_at_beginning(10)
ll.insert_at_beginning(20)
ll.insert_at_beginning(30)
print("List after inserting at beginning:", ll.display())
List after inserting at beginning: [30, 20, 10]
Adding Elements at the End
When adding at the end, we traverse to the last node and update its next pointer to the new node ?
class LinkedList:
def __init__(self):
self.head = None
def insert_at_end(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
def display(self):
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements
# Example usage
ll = LinkedList()
ll.insert_at_end(10)
ll.insert_at_end(20)
ll.insert_at_end(30)
print("List after inserting at end:", ll.display())
List after inserting at end: [10, 20, 30]
Adding Elements at a Specific Position
To insert at a specific position, we traverse to the desired location and adjust the pointers ?
class LinkedList:
def __init__(self):
self.head = None
def insert_at_position(self, data, position):
if position == 0:
self.insert_at_beginning(data)
return
new_node = Node(data)
current = self.head
for i in range(position - 1):
if current is None:
print("Position out of bounds")
return
current = current.next
new_node.next = current.next
current.next = new_node
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def display(self):
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements
# Example usage
ll = LinkedList()
ll.insert_at_beginning(10)
ll.insert_at_beginning(20)
ll.insert_at_position(15, 1) # Insert 15 at position 1
print("List after inserting at position 1:", ll.display())
List after inserting at position 1: [20, 15, 10]
Complete Implementation
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def insert_at_end(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
def insert_at_position(self, data, position):
if position == 0:
self.insert_at_beginning(data)
return
new_node = Node(data)
current = self.head
for i in range(position - 1):
if current is None:
print("Position out of bounds")
return
current = current.next
new_node.next = current.next
current.next = new_node
def display(self):
if not self.head:
print("List is empty")
return
current = self.head
print("Linked List:", end=" ")
while current:
print(current.data, end=" -> " if current.next else " -> None\n")
current = current.next
# Example demonstration
ll = LinkedList()
ll.insert_at_beginning(10)
ll.insert_at_end(20)
ll.insert_at_position(15, 1)
ll.display()
Linked List: 10 -> 15 -> 20 -> None
Comparison of Insertion Methods
| Method | Time Complexity | Space Complexity | Use Case |
|---|---|---|---|
| Insert at Beginning | O(1) | O(1) | Stack operations |
| Insert at End | O(n) | O(1) | Queue operations |
| Insert at Position | O(n) | O(1) | Ordered insertion |
Conclusion
Linked lists provide efficient insertion operations with different time complexities based on position. Use beginning insertion for O(1) performance, and position-based insertion when order matters. The dynamic nature makes linked lists ideal for scenarios where the size varies frequently.
