# Search an element in a Linked List (Iterative and Recursive)

0 like 0 dislike
1.9k views

How to find an element in a linked list? Here We will see Python Program to search an element in a linked list.

0 like 0 dislike
by Goeduhub's Expert (2.2k points)
edited

## 1) Search an element in LinkedList (iterative approach)

There are various steps included in the iterative approach for search an element in a linked list and they are as follows:

Step:2 As current-> key is not equal to given key,current=current->next(2nd node).

Step:3 As current->key is not equal to given key,current=current->next(3rd node).

Step:4 As current->key is equal to a given key,return True.

# Node class

class Node:

# Function to initialize the node object

def __init__(selfdata=Nonenext=None):

self.data = data # Assign data

self.next = next # Initialize next as null

# Function to initialize the LinkedList object

def __init__(self):

def print(self):

# if linked list is empty

return

#taking empty string and appending values

llstr = ''

while itr:

llstr += str(itr.data)+' --> '

itr = itr.next

print(llstr)

# Function to insert a new node at the beginning

def insert_at_begining(selfdata):

# This Function checks whether the value x present in the linked list

def search_element(self,x):

count=0

# loop till itr not equal to None

while itr!=None

if itr.data == x:

return count # data found

itr = itr.next

count=count+1

return False # Data Not found

# Code execution starts here

if __name__ == '__main__':

ll.insert_at_begining(10)

ll.insert_at_begining(20)

ll.insert_at_begining(30)

ll.insert_at_begining(40)

ll.print()

result=ll.search_element(20)

if (result>=0):

print("element found in Linkedlist at %d index",result)

else:

print("element not found")

## 2) Search an element in LinkedList (Recursive approach)

Various steps included in this approach are as follows:

Step:3 Again Head is not Null and head->key is equal to given key.Therefore,True is returned.

# Node class

class Node:

# Function to initialize the node object

def __init__(selfdata=Nonenext=None):

self.data = data # Assign data

self.next = next # Initialize next as null

# Function to initialize the LinkedList object

def __init__(self):

def print(self):

# if linked list is empty

return

#taking empty string and appending values

llstr = ''

while itr:

llstr += str(itr.data)+' --> '

itr = itr.next

print(llstr)

# Function to insert a new node at the beginning

def insert_at_begining(selfdata):

# This Function checks whether the value x present in the linked list

def search_element(self,itr,x):

# if linked list is empty

if itr is None:

return False

# If key is present in current node, return true

if(itr.data == x):

return True

# Recur for remaining list

return self.search_element(itr.next,x)

# Code execution starts here

if __name__ == '__main__':

ll.insert_at_begining(10)

ll.insert_at_begining(20)

ll.insert_at_begining(30)

ll.insert_at_begining(40)

ll.print()

if (result):