Share my python code


  • 0
    N
    class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        iter_node = new_head = ListNode(0)
        new_head.next = head
        
        while iter_node.next is not None and iter_node.next.next:
            temp_node = iter_node.next.next   # 1
            iter_node.next.next = temp_node.next # 2-> = 1->
            temp_node.next = iter_node.next     #1-> = 2
            iter_node.next = temp_node   #head ->   1
            iter_node = iter_node.next.next
            
        return new_head.next

Log in to reply
 

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