Simple Python solution


  • 0
    S

    Recursive where each call swaps a pair of nodes and recursively make another call to swap the rest of the list

    class Solution(object):
        def swapPairs(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head is None or head.next is None:
                return head
            n = self.swapPairs(head.next.next)
    
            second = head.next
            second.next = head
            head.next = n
            return second
        ```

Log in to reply
 

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