Short'n'Sweet Python Solution with O(1) memory


  • 0
    class Solution(object):
        def oddEvenList(self, head):
            if not head or not head.next:
                return head
            odd, even, h, tail = head, head.next, head.next, None
            while odd and even:
                odd.next = even.next
                tail = odd
                odd = odd.next
                even.next = odd.next if odd else None
                even = even.next
            tail = odd if odd else tail
            tail.next = h
            return head

Log in to reply
 

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