0% found this document useful (0 votes)
5 views

Single Linked List (1)

Uploaded by

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

Single Linked List (1)

Uploaded by

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

Linked List

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.

Search: Search a node containing particular value in the linked list.


Insertion to a Single Linked list

Add a new node at the first, last or interior of a linked list.

10
Insertion to a Single Linked list

Add a new node at the first, last or interior of a linked list.

10
Insertion to a Single Linked list

Add a new node at the first, last or interior of a linked list.

10
Insertion to a Single Linked list

Add a new node at the first, last or interior of a 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

class LinkedList: Step 1 10


def __init__(self):
self.head = None
newitem head
def insertAtBegin(self, data):
newitem = Node(data) Step 2 10
if self.head is None:
self.head = newitem newitem
return 10
Step 3
else:
head
newitem.next = self.head
self.head = newitem
Insert Last

 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

head last newitem

Step 2

head last newitem

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

head current newitem

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

if current != None: head current


newitem = Node(data) Step 2 num
newitem.next = current.next
current.next = newitem head current newitem
else: Step 3 num
print(“number not found")
Printing List

def printLL(self):
current = self.head
while(current):
print(current.data)
current = current.next
Deletion from a Single Linked list

Deletion can be done


 At the first node of a linked list.
 At the end of a linked list.
 Within the linked list.
Deletion from a Single Linked list

Deletion can be done


 At the first node of a linked list.
 At the end of a linked list.
 Within the linked list.
Deletion from a Single Linked list

Deletion can be done


 At the first node of a linked list.
 At the end of a linked list.
 Within the linked list.
Delete First

 Designate the second node of the list as the head.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy