Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.29 KB

024._swap_nodes_in_pairs.md

File metadata and controls

52 lines (42 loc) · 1.29 KB

24. Swap Nodes in Pairs

题目: https://leetcode.com/problems/swap-nodes-in-pairs/

难度 : Medium

一眼就知道这个用递归做,beats 96.06%

class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        if not head.next:
            return head
        tmp = head.next
        head.next = self.swapPairs(head.next.next)
        tmp.next = head
        return tmp

或者用loop做,每个node关系要弄清楚, 又是巧用dummydummy大法对于nodeList的题目简直无敌!!!🐂批, 但是只beats69.40%

class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None or head.next == None:
            return head

        dummy = ListNode(-1)
        dummy.next = head
        
        cur = dummy

        while cur.next and cur.next.next:
            next_one, next_two, next_three = cur.next, cur.next.next, cur.next.next.next
            cur.next = next_two
            next_two.next = next_one
            next_one.next = next_three
            cur = next_one
        return dummy.next