Linear Linked List – Search Element using Python

What’s up friends?!?

Let’s continue on another linear linked list problem. I’m going to make this post short since I’ll use the boilerplate code from the last LLL problem we had. If you need any help, I’ll also post the link to GitHub so you can stroll along.

Anyways, for this problem, I just want to write some code to find a specific element inside the LLL. You could do this iteratively or recursively, whichever way you choose to do, but for the purpose of this program, I am just going to solve it iteratively.

    # searching iteratively function
    def searchElement(self, number):

    # default case if there's no list in the first place
    if not self.head:
        return False

    current = self.head

    # going through the linked list
    while current is not None:
        if current.data == number:
            return True
        current = current.next

    return False

I’d say the most important here is to notice how I used the while loop to go through each of the node, and use the pointers to point to the next node. It would exit out of the loop if the data of the current node doesn’t exist.

Well, if you have any question, feel free to ask and have a great day.

 

Written by Alex Mercer

Alex Mercer is a writer and contributor at Project Immerse, covering web design, WordPress, web development, SEO, digital strategy, emerging technology, and the evolving world of the web. His work focuses on practical ideas, useful tools, and approachable guidance that help creators, developers, and business owners build better digital experiences.

Alex writes with a hands-on, curiosity-driven approach, exploring both established web practices and new technologies shaping how websites are designed, built, and discovered.