My 7-line simple python solution


  • 1
    N
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head
        fnext = head.next
        fnnext = fnext.next
        head.next = self.swapPairs(fnnext)
        fnext.next = head
        return fnext

Log in to reply
 

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