implementing a find node function for a singly linked list

You’re right. You need to look at dataval and not the index:

  def find(self, val):
    current=self.headval 
    count=0
    while current != None:
      if (current.dataval==val):
        return count
      count+=1
      current=current.nextval
    assert(False)
    return -1      # return -1 instead of 0

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top