借助新增加的结点
class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if not head: return if head.next==None: return head a=ListNode(0) p=head q=p.next head=a while p and q: a.next=q p.next=q.next q.next=p a=p p=p.next if p!=None: q=p.next return head.next
class Solution: def swapPairs(self, head: ListNode) -> ListNode: thead = ListNode(-1) thead.next = head c = thead while c.next and c.next.next: a, b=c.next, c.next.next c.next, a.next = b, b.next b.next = a c = c.next.next return thead.next 作者:wu-yan-34 链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/solution/bi-jiao-zhi-jie-gao-xiao-de-zuo-fa-han-tu-jie-by-w/
原文:https://www.cnblogs.com/taoyuxin/p/11736494.html