0% found this document useful (0 votes)
4 views2 pages

Linked List Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Linked List Notes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Linked List Notes with Python Programs

1. Introduction
Linked List is a linear data structure where elements (called nodes) are connected using pointers/references.
Each node contains:
- Data → the actual value
- Pointer/Reference → address of the next node

Unlike arrays:
- Memory is not contiguous.
- Size is dynamic.
- Insertions/Deletions are faster (compared to arrays).

2. Types of Linked Lists - Singly Linked List (SLL)


Each node has data + next pointer.

Python Implementation:

class Node:
def __init__(self, data):
self.data = data
self.next = None

class SinglyLinkedList:
def __init__(self):
self.head = None

def display(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")

def insert_begin(self, data):


new_node = Node(data)
new_node.next = self.head
self.head = new_node

def insert_end(self, data):


new_node = Node(data)
if not self.head:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node

def delete_begin(self):
if self.head:
self.head = self.head.next

def delete_end(self):
if not self.head or not self.head.next:
self.head = None
return
temp = self.head
while temp.next.next:
temp = temp.next
temp.next = None

# Example usage
ll = SinglyLinkedList()
ll.insert_end(10)
ll.insert_end(20)
ll.insert_begin(5)
ll.display()
ll.delete_begin()
ll.display()
ll.delete_end()
ll.display()

You might also like