算法训练营day4 LC24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II

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

class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy = ListNode(0)
        first = head
        dummy.next = first
        if not first:
            return first
        elif not first.next:
            print(first)
            return first
        newHead = first.next
        while first and first.next:
            temp = first.next.next
            first.next.next = first
            dummy.next = first.next
            dummy = first
            first.next = temp
            first = temp
        return newHead

你可能感兴趣的:(链表,算法,数据结构)