1. 程式人生 > >python leetcode 24. Swap Nodes in Pairs

python leetcode 24. Swap Nodes in Pairs

class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """ 
        ans=ListNode(0)
        res=ans
        ans.next=head
        while head and head.next:
            p=head.next
            head.next=p.next
            ans.
next=p p.next=head ans=head head=head.next return res.next