6 lines in Python with recursion, easy to understand


  • 0
    J
    class Solution(object):
        def swapPairs(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head == None or head.next == None:
                return head
            second_node = head.next
            head.next = self.swapPairs(second_node.next)
            second_node.next = head
            return second_node
    

    It takes about 40ms, not very fast :(


Log in to reply
 

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