Single Linked List (1)
Single Linked List (1)
DSA 1
Faculty: Rubaiya Rahtin Khan
Head
5 7 3 1
Linked List
A linked list is a linear collection of data elements (called nodes),
where the linear order is given by means of links.
Each node is divided into 2 parts:
1st part contains the information of the element.
2nd part is called the link field or next field which contains the link to the
next node in the list. Head
class Node:
5 7 3 1
def __init__(self, data):
self.data = data
self.next = None
Basic Operations
Insert: Add a new node in the first, last or interior of the list.
Delete: Delete a node from the first, last or interior of the list.
10
Insertion to a Single Linked list
10
Insertion to a Single Linked list
10
Insertion to a Single Linked list
10
Insert First
To add a new node to the head of the linear linked list, we need to
create a new node that is called newitem.
If the head is empty then we make the newitem as head.
Otherwise the newitem points to the first node in the list. The head is
then set to point to the newitem.
Insert First (Cont.)
Step 1. Create a new node called newitem.
Step 2. Link the new node to the first node of the linked list.
Step 3. Assign the newitem as head..
newitem head
Step 1
newitem head
Step 2
newitem
Step 3
head
Insert First (Cont.)
newitem
head
To add a new node to the tail of the linear linked list, we need to
create a new node and set it's link field to None.
Assume the list is not empty, locate the last node and change it's link
field to point to the new node.
Insert Last (Cont.)
Step1. Create the new node.
Step2. Find the last node.
Step3. Link the last node to the new node.
head newitem
Step 1
Step 2
Step 3
Insert Last (Cont.)
head newitem
Step 1 1
def inserAtEnd(self, data): 0
newitem = Node(data) head current newitem
if self.head is None:
Step 2 1
self.head = newitem 0
return head current newitem
Step 3 1
current = self.head
0
while(current.next):
current = current.next
current.next = newitem
Insert in the Middle (after a desired node)
Step 1. Find the desired node with data value num.
Step 2. Label the node as current.
Step 3. Insert the newitem after current. newitem
head
Step 1 num
newitem
head current
Step 2 num
Step 3 num
Insert in the Middle (after a desired
node)
newitem
def insertAfter(self, data, num):
current = self.head head
while (current != None and current.data!= num):
Step 1 num
current = current.next
newitem
def printLL(self):
current = self.head
while(current):
print(current.data)
current = current.next
Deletion from a Single Linked list
head
head
Delete First(Cont.)
def remove_first_node(self):
if(self.head == None): head
return
self.head = self.head.next
head
Delete Last
Step 1. Find the second last node.
Step 2. Set its next link to None.
head
curren
t
head current
Step 1
head current
Step 2
Delete Last(Cont.)
head
def remove_last_node(self):
curren
t
if self.head == None:
head current
return
Step 1
current = self.head
while (current.next != None and current.next.next != None):
current = current.next current
head
Step 2
current.next = None
Delete Any(Node with a specific
value)
Step 1. Find the node before the node to be deleted.
Step 2. Set the previous nodes next link to the node after the node to
hea
be deleted.
d
nu
m
hea current
d
nu
m
hea current
d
nu
m
Delete Any(Node with a specific value)
def removeAny(self, num):
if self.head==None:
return
if self.head.data==num:
self.head=self.head.next
return
current = self.head
while current.next!=None and current.next.data!=num:
current = current.next
if current.next==None:
print(“Number not found")
else:
current.next = current.next.next