Python solution using 36ms with simple method


  • 4
    A

    class Solution(object):

    def swapPairs(self, head):
    
        if not head or not head.next:
            return head
      
        first,second = head, head.next
        third = second.next
        head = second
        second.next = first
        first.next = self.swapPairs(third)
        
        return head

Log in to reply
 

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