My Python recursive solution without using any extra space


  • 0
    S
    def swapPairs(self, head):
    		if not head:
    			return None
    		elif not head.next:
    			return head
    		else:
    			nextNode = head.next
    			head.next = self.swapPairs(nextNode.next)
    			nextNode.next = head
    			return nextNode

Log in to reply
 

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