My clear recursion python solution


  • 0
    G
    enter code hereclass Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        p0=dummy=ListNode(0)
        dummy.next=head
        self.helper(p0)
        return p0.next
    
    def helper(self,p0):
        if p0.next and p0.next.next:
            p1=p0.next
            p2=p1.next
            
            p0.next=p2
            p1.next=p2.next
            p2.next=p1
            
            self.helper(p1)

Log in to reply
 

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