Easy python solution

  • 0

    class Solution(object):

    def swapPairs(self, head):
        :type head: ListNode
        :rtype: ListNode
        dummy = ListNode(None)
        dummy.next = head
        ptr = dummy
        while ptr.next:
            first = ptr.next
            second = first.next
            if not second:
            third = second.next
            ptr.next = second
            second.next = first
            first.next = third
            ptr = first
        return dummy.next

Log in to reply

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