首页 > 其他 > 详细

24. 两两交换链表中的节点

时间:2020-05-16 14:20:43      阅读:44      评论:0      收藏:0      [点我收藏+]

技术分享图片
技术分享图片

代码一:

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

代码二:

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

24. 两两交换链表中的节点

原文:https://www.cnblogs.com/panweiwei/p/12899906.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!