python-leetcode-两两交换链表中的节点
24. 两两交换链表中的节点 - 力扣(LeetCode)
# 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]:
if not head or not head.next:
return head
dummy = ListNode(0)
dummy.next = head
prev, curr = dummy, head
while curr and curr.next:
nxt = curr.next
curr.next = nxt.next
nxt.next = curr
prev.next = nxt
prev = curr
curr = curr.next
return dummy.next