title: Finding middle of double linked list for even number of nodes
body: <p>Following is my code to find middle node of double linked list but it works only for odd number of nodes. i cannot figure out how to take care of case of even number of nodes:</p>
  
  <pre><code>public Object findMiddleNode() {
      DLNode first = getHeadNode();
      DLNode last = getTailNode();
      while(first!=last) {
          first = first.getNext();
          last = last.getPrevious();
      }
      return first.getElement();      
  }
  </code></pre>
  
  <hr>
  
  <p>this is my updated code:</p>
  
  <pre><code>public MyDequeue findMiddleNode() {
      DLNode first = getHeadNode();
      DLNode last = getTailNode();
      MyDequeue m = new MyDequeue();
      while(first!=last &amp;&amp; first.getNext()!= last) {
          first = first.getNext();
          last = last.getPrevious();
      }
      if(first == last) {
          m.insertLast(first.getElement());           
          return m;
      }           
      else {
          m.insertLast(first.getElement());
          m.insertLast(last.getElement());
          return m;
      }
  }
  </code></pre>
  
  <p>the class MyDequeue is my implementation of a doubly linked list. the reason I returned MyDequeue is because I already had a static print method that prints the elements of a doubly linked list.</p>