原题链接:https://leetcode.com/problems/swap-nodes-in-pairs/
解题思路:在head前添加一个node,第一步,将pre指向该对pair的第二个,第二步,将cur指向下一对pair的第一个,第三步,将该对pair的第二个指向第一个。
【leetcode刷题】24. Swap Nodes in Pairs代码:

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

        pre = ListNode(0)
        pre.next = head
        current = head
        head = pre
        while current:
            pre.next = current.next
            current.next = current.next.next
            pre.next.next = current
            pre = pre.next.next
            current = current.next
        return head.next

相关文章:

  • 2021-07-12
  • 2021-12-07
  • 2021-05-31
  • 2022-03-10
  • 2021-09-04
  • 2021-12-28
  • 2022-02-16
猜你喜欢
  • 2021-06-02
  • 2021-09-24
  • 2021-10-31
  • 2022-01-26
  • 2021-07-05
  • 2021-06-27
相关资源
相似解决方案