Python simple solution O(1) space O(n) time


  • 0
    A
    class Solution(object):
        def oddEvenList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head: return head
            
            odd = rootodd = head
            even = rooteven = head.next
            
            while even and even.next:
                n1 = even.next
                n2 = even.next.next
                odd.next = n1
                even.next = n2
                odd, even = n1, n2
                
            odd.next = rooteven
            return rootodd
            
    

Log in to reply
 

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