educative.io

Simpler implementation?

This solution looks a little more simple than Solution 2.
I would appreciate a review - is there anything missing or less efficient?

def findMid(list):
  slow = list.getHead()
  if slow.nextElement == None:
    return # assuming that None is an acceptable return for edge cases
  fast = list.getHead()

  while (slow and fast and fast.nextElement != None):
    slow = slow.nextElement
    fast = fast.nextElement.nextElement
  
  return slow.data

Thanks!!

Hi dennis,

Thank you for your valuable input. The complexity of this solution is the same as solution 2. This one looks simpler because the conditional statements have been grouped together. You can go ahead with your code if you want!

Regards,
Coderust