Python Solution (Same as Partition List)


  • 0
    L

    I just reused the solution of Partition List problem present here.
    Sharing as original solution was easier to remember and two birds with one stone.

    class Solution(object):
        def oddEvenList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            lefthead = left = ListNode(0)
            righthead = right = ListNode(0)
            
            count = 1
            while head:
                if count%2:
                    left.next = head
                    left = left.next
                else:
                    right.next = head
                    right = right.next
                head=head.next
                count+=1
            right.next = None
            left.next = righthead.next
            return lefthead.next
    

Log in to reply
 

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