Short Python with two dummy nodes


  • 0

    The description challenged my language. I was so fxxked up when saw [1, 3, 5, 7, 2, 4, 6, 8] is wrong. However, I figured it out and solved it with the almost the same code used in Partition List.

    class Solution(object):
        def oddEvenList(self, head):
            headOdd = dummyOdd = ListNode(-1)
            headEven = dummyEven = ListNode(-1)
            count = 1
            while head:
                if count % 2 == 0:
                    dummyEven.next = head
                    dummyEven = dummyEven.next
                else:
                    dummyOdd.next = head
                    dummyOdd = dummyOdd.next
                head = head.next
                count += 1
            dummyEven.next = None
            dummyOdd.next = headEven.next
            return headOdd.next

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.