给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummyhead = ListNode()
dummyhead.next = head
cur = dummyhead
while cur.next != None and cur.next.next != None:
temp3 = cur.next.next.next
temp1 = cur.next
cur.next = cur.next.next
cur.next.next = temp1
temp1.next = temp3
cur = cur.next.next
return dummyhead.next