My Python AC Code with 40ms.


  • 0
    W
    class Solution(object):
        def swapPairs(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            p = head
            if p is None or p.next is None:
                return p
            q = p.next
            head = q
            pre = ListNode(-1)
            while p and q:
                t = q.next
                q.next = p
                p.next = t
                pre.next = q
                pre = p
                if t and t.next:
                    p = t
                    q = t.next
                else:
                    break
            return head

Log in to reply
 

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