title: (Python) Singly LinkedList - Leetcode
body: <p>I am working on this Leetcode problem <a href="https://leetcode.com/problems/odd-even-linked-list/" rel="nofollow noreferrer">https://leetcode.com/problems/odd-even-linked-list/</a>. 
  I already tried to debug the program with the debugging tool and found the bug in my code but I don't really understand the bug and I don't really know how to fix it. The bug came from this line: <code>odd_head.next = even_head</code>
  Thanks for your help!</p>
  
  <p>The problem is:
  Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.</p>
  
  <pre><code>For example:
  Input: 2-&gt;1-&gt;3-&gt;5-&gt;6-&gt;4-&gt;7-&gt;NULL
  Output: 2-&gt;3-&gt;6-&gt;7-&gt;1-&gt;5-&gt;4-&gt;NULL
  
  Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL
  Output: 1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL
  </code></pre>
  
  <pre><code>def oddEvenList(self, head):
      odd_head = head
      even_head = head.next
  
      while(odd_head.next and odd_head.next.next):
          temp = odd_head.next.next
          odd_head.next = temp
          odd_head = temp
  
      odd_head.next = even_head # BUG ON THIS LINE
  
      while(even_head.next and even_head.next.next):
          temp = even_head.next.next
          even_head.next = temp
          even_head = temp
  
      return odd_head
  </code></pre>