easy python solution

  • 0
    def swapPairs(self, head):
        if head is None or head.next is None:
            return head
        newHead, lastHead = head.next, None
        while head:
            cur = head.next
            if cur is None:
            head.next, cur.next = cur.next, head
            if lastHead:
                lastHead.next = cur
            lastHead, head = head, head.next
        return newHead

Log in to reply

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