My python solution with small space


  • 0
    W
    def swapPairs(self, head):
        if not head: return None
        if not head.next: return head
        ret_node = head.next
        while True:
            temp = head.next.next
            head.next.next = head
            if not temp or not temp.next:
                head.next = temp
                break
            else:
                head.next = temp.next
                head = temp
        return ret_node

Log in to reply
 

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