LAB - Python 01
LAB - Python 01
28/12/23
Introduction to Linked Lists
• class Node:
• def __init__(self, data):
• self.data = data
• self.next = None
Creating a Linked List
• class LinkedList:
• def __init__(self):
• self.head = None
• def display(self):
• current = self.head
• while current:
• print(current.data, end=" -> ")
• current = current.next
• print("None")
Insertion at the Beginning
• def delete_at_beginning(self):
• if self.head:
• self.head = self.head.next
• else:
• print("List is empty. Nothing to delete.")
Deletion at a Specific Position
• def delete_at_position(self, position):
• if position == 0:
• self.delete_at_beginning()
• else:
• current = self.head
• for _ in range(position - 1):
• if current is None or current.next is None:
• print("Position out of bounds.")
• return
• current = current.next
• current.next = current.next.next
Thank You