Share python,easy to unserstand


  • 0
    class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:return head
        p,q=head,head.next
        r=q#r是新的头结点
        while p and q:
            k=p#
            p.next=q.next
            q.next=p
            if p.next and p.next.next:
                p=p.next
                q=p.next
                k.next=q
            else:
                break
        return r

Log in to reply
 

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