easy understand solution

  • 0

    class Solution(object):
    def swapPairs(self, head):
    :type head: ListNode
    :rtype: ListNode
    node = ListNode(0)
    node.next = head
    head = node

        while head.next and head.next.next:
            node1, node2 = head.next, head.next.next
            head.next, node1.next, node2.next = node2, node2.next, node1
            head = node1
        return node.next


Log in to reply

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