Python, 7 lines, no dummy, no pre-check


  • 0
    S
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        first, second = head, head
        while first and first.next:
            second = second.next
            first.val, second.val = second.val, first.val
            first = second.next
            second = first
        return head

  • 0
    M

    Clever, but you're not actually swapping the nodes here. That being said, they didn't say you couldn't just swap the values.


  • 0
    S

    @mjeffe6 You'r right. I didn't notice that. My answer is not standard enough... Thanks for reminding me. :)


Log in to reply
 

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