Question

In Python 3 Write a LinkedList method named contains, that takes a value as a parameter...

In Python 3 Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python method that you asked for:

    def search(self, x):

        current = self.head

        while current != None:

            if current.data == x:

                return True #Return True if data is found

            current = current.next

        return False # Return False if data Not found

Full python program:

class Node:

    def __init__(self, data):

        self.data = data

        self.next = None

class LinkedList:

    def __init__(self):

        self.head = None

    def push(self, new_data):      

        new_node = Node(new_data)

        new_node.next = self.head

        self.head = new_node

       

    def search(self, x):

        current = self.head

        while current != None:

            if current.data == x:

                return True #Return True if data is found

            current = current.next

        return False # Return False if data Not found

#Main method

linklist = LinkedList()

linklist.push(1);

linklist.push(2);

linklist.push(3);

linklist.push(4);

linklist.push(5);

if linklist.search(1):

    print("True")

else:

    print("False")

if linklist.search(6):

    print("True")

else:

    print("False")

Code screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
In Python 3 Write a LinkedList method named contains, that takes a value as a parameter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT