title: How to find middle element in a python linked list in a single traversal?
body: <p>Sorry for asking such question(new to programming):</p>
  
  <p>I want to find the middle element of linked list using <strong>findMid</strong> method. Sorry for poor explanation because English is not my native language. thanks :) </p>
  
  <p>My code is creating the linked list and I want to find the middle element of the linked list using a single traversal. So far i've implemented one function using pointer concept by seeking help by google and that function is:</p>
  
  <pre><code>def findMid(self):
      slowPtr = self.__head
      fastPtr = self.__head
      if not self.__head is not None:
          while fastPtr is not None and fastPtr.next is not None:
              fastPtr = fastPtr.next.next
              slowPtr = slowPtr.next
          return slowPtr
  </code></pre>
  
  <p>but its returning me <strong>None</strong></p>
  
  <p>and My rest of linked list code is:</p>
  
  <pre><code> class LinkedList(object):
  
          class Node(object):
              def __init__(self, element,next=None):
                  self.element = element
                  self.next = next
                  # method returns address of the next Node
          def __init__(self,initial=None):
              self.__head = None
              self.__tail = None
              self.__size = 0
              if initial is not None:
                  self.add(initial)
  
          **def findMid(self):
              slowPtr = self.__head
              fastPtr = self.__head
              if not self.__head is not None:
                  while fastPtr is not None and fastPtr.next is not None:
                      fastPtr = fastPtr.next.next
                      slowPtr = slowPtr.next
                  return slowPtr**
  
  
  
          # Return the head element in the list 
          def getFirst(self):
              if self.__size == 0:
                  return None
              else:
                  return self.__head.element
  
          # Return the last element in the list 
          def getLast(self):
              if self.__size == 0:
                  return None
              else:
                  return self.__tail.element
  
          # Add an element to the beginning of the list 
          def addFirst(self, e):
              newNode = self.Node(e) # Create a new node
              newNode.next = self.__head # link the new node with the head
              self.__head = newNode # head points to the new node
              self.__size += 1 # Increase list size
  
              if self.__tail == None: # the new node is the only node in list
                  self.__tail = self.__head
  
          # Add an element to the end of the list 
          def addLast(self, e):
              newNode = self.Node(e) # Create a new node for e
  
              if self.__tail == None:
                  self.__head = self.__tail = newNode # The only node in list
              else:
                  self.__tail.next = newNode # Link the new with the last node
                  self.__tail = self.__tail.next # tail now points to the last node
  
              self.__size += 1 # Increase size
  
          # Same as addLast 
          def add(self, e):
              self.addLast(e)
  
          # Insert a new element at the specified index in this list
          # The index of the head element is 0 
          def insert(self, index, e):
              if index == 0:
                  self.addFirst(e) # Insert first
              elif index &gt;= self.__size:
                  self.addLast(e) # Insert last
              else: # Insert in the middle
                  current = self.__head
                  for i in range(1, index):
                      current = current.next
                  temp = current.next
                  current.next = self.Node(e)
                  (current.next).next = temp
                  self.__size += 1
  
          # Return true if the list is empty
          def isEmpty(self):
              return self.__size == 0
  
          # Return the size of the list
          def getSize(self):
              return self.__size
  
          def __str__(self):
              result = ""
  
              current = self.__head
              for i in range(self.__size):
                  result += str(current.element)
                  current = current.next
                  if current != None:
                      result += ", " # Separate two elements with a comma
              result = re.sub('[\(\)\{\}&lt;&gt;]', '', result)
              return result
  
          # Clear the list */
          def clear(self):
              self.__head = self.__tail = None
  
          # Return elements via indexer
          def __getitem__(self, index):
              return self.get(index)
  
          # Return an iterator for a linked list
          def __iter__(self):
              return LinkedListIterator(self.__head)
  class LinkedListIterator:
      def __init__(self, head):
          self.current = head
  
      def __next__(self):
          if self.current == None:
              raise StopIteration
          else:
              element = self.current.element
              self.current = self.current.next
              return element
  </code></pre>