My python code,O(n)time defeat 91%


  • 0
    W
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def oddEvenList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            firstodd=ListNode(0)
            firsteven=ListNode(0)
            firstodd.next=firsteven
            firsteven.next=head
            preodd=firstodd
            preeven=firsteven
            curodd=head
            while curodd and curodd.next:
                cureven=curodd.next
                preodd.next=curodd
                preeven.next=cureven
                preodd=curodd
                preeven=cureven
                curodd=cureven.next
            preeven.next=None
            if curodd:
                preodd.next=curodd
                curodd.next=firsteven.next
            else:
                preodd.next=firsteven.next
            return firstodd.next
    

Log in to reply
 

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