I am working on this Leetcode problem https://leetcode.com/problems/odd-even-linked-list/.
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: odd_head.next = even_head
Thanks for your help!
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.
For example:
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
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