leetcode -- Swap Nodes in Pairs -- 简单重点

https://leetcode.com/problems/swap-nodes-in-pairs/

没什么技巧。注意不要出错。交换node的操作要仔细。

class Solution(object):
    def swapPairs(self, head):
        """ :type head: ListNode :rtype: ListNode """
        if not head: return head

        dummy = ListNode(0)
        dummy.next = head
        pre_i = dummy
        i = head
        tmp = None
        while i and i.next:
            tmp, i.next.next = i.next.next, i
            pre_i.next = i.next
            i.next = tmp
            pre_i, i = i, tmp
        return dummy.next

你可能感兴趣的:(LeetCode)