A clear Python solution


  • 0
    M

    Two pointers pointing to the current odd and even nodes. At the end, link the last odd node to the head of even nodes. Boom! Done.

    def oddEvenList(self):
        if head and head.next:
            curOdd = head
            headEven, curEven = head.next, head.next
            while curOdd and curEven and curOdd.next and curEven.next:
                curOdd.next = curEven.next
                curOdd = curOdd.next
                curEven.next = curOdd.next
                curEven = curEven.next
            curOdd.next = headEven
        return head

Log in to reply
 

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