9-Line, 99% beat Concise Python AC Code

  • 0

    Same logic as many of the top solutions, but uses Python's sequence unpacking for more concise and performant code.

    class Solution(object):
        def reorderList(self, head):
            if not head: return
            slow, fast = head, head
            while fast and fast.next:
                slow, fast = slow.next, fast.next.next
            pre, slow.next, slow = None, None, slow.next
            while slow:
                slow.next, pre, slow = pre, slow, slow.next
            while head and pre:
                head.next, head, pre.next, pre = pre, head.next, head.next, pre.next

Log in to reply

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