10 lines of Python, O(1) space, O(n) time, In Place, 1 While loop

  • 0
    def oddEvenList(self, head):
        if head == None: return head
        oddTail = head; evenTail = ListNode(0); evenTail.next = head.next
        while evenTail.next != None and evenTail.next.next != None:
            tmp = evenTail.next.next
            evenTail.next.next = tmp.next
            evenTail = evenTail.next
            tmp.next = oddTail.next
            oddTail.next = tmp
            oddTail = oddTail.next
        return head

Log in to reply

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