Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

How to search in a singly linked list

P.Praneet

We can search in a singly linked list (SLL) using the linear search technique.

Creating a singly list

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

We need some elements in the SLL so we add them as follows:

  • insertstart is used to insert elements from one side.

  • insertend is used to insert elements from the other side.

data is the element that we add

def insertstart(self,data):# inserts the element as head
        new_node= Node(data)
        new_node.next=self.head
        self.head = new_node
    
def insertend(self,data):
        new_node = Node(data)
        if self.head is None:# checks if the SLL is empty 
            self.head=new_node# if it is empty it adds element as head
            return
        end = self.head
        while(end.next):# if the SLL is not empty it adds the element as tail
            end = end.next
        end.next = new_node

The search method checks if the SLL is empty:

def search(self,data):
        if self.head == None: 
        # This condition checks if a SLL 
            print("we need a linked list")
        

Then, the SLL is searched. In our process we used the linear search technique.

Here data is the element to be searched.

gap is the element of the SLL that is compared with data.

        gap = self.head # first gap is assigned the head element
        # here gap is looped and assignment each and every element of SLL    
        while gap:
            if gap.data==data: 
                # it checks for the element data and returns true if found
                break
            gap = gap.next
        if gap==None:
            # if the required element data is not found then following statement is returned 
            print("given data does not exist")
            return
        return True

Code

class Node: 
    def __init__(self, data): 
        self.data = data   
        self.next = None   
class LinkedList: 
    def insertstart(self,data):# inserts the element as head
        new_node= Node(data)
        new_node.next=self.head
        self.head = new_node
    
    def insertend(self,data):
        new_node = Node(data)
        if self.head is None:# checks if the SLL is empty 
            self.head=new_node# if it is empty it adds element as head
            return
        end = self.head
        while(end.next):# if the SLL is not empty it adds the element as tail
            end = end.next
        end.next = new_node
    def search(self,data):
        if self.head == None: 
        # This condition checks if a SLL 
            print("we need a linked list")
            gap = self.head # first gap is assigned the head element
        # here gap is looped and assignment each and every element of SLL    
        gap = self.head
        while gap:
            if gap.data==data: 
                # it checks for the element data and returns true if found
                print("The required element is found")
                return True
            gap = gap.next
            
        if gap==None:
            # if the required element data is not found then following statement is returned 
            print("Required element does not exist")
            return
        return True
#All the Linked list test cases
sll = LinkedList()
sll.head = Node(9)

sll.search(9)# True case
sll.search(8)# False case
Code to implement

RELATED TAGS

python
community creator
RELATED COURSES

View all Courses

Keep Exploring