Recursive? Why not using a dequeue? Short and clean solution gain

  • 0

    Using recursive solution means an O(N) calling stack, which is essentially the same and less readable.\

    class Solution:
        # @param head, a ListNode
        # @return nothing
        def reorderList(self, head):
            queue = collections.deque()
            while head:
                head =
            dummy = ListNode(-1)
            head = dummy
            length = 0
            while queue:
                if length % 2 == 0:
           = queue.popleft()
           = queue.pop()
                head =
                length += 1
   = None

  • -1

    Because the task is to use constant space (in-place).

  • 0

    I don't think constant space is possible, unless this is a doubly linked list which it is not.
    Btw, I did it with deque, perfect and clean and accepted.

Log in to reply

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