Swap Nodes in Pairs
Solution Code
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, head)
prev, curr = dummy, head
while curr and curr.next:
nxtPair = curr.next.next
second = curr.next
# Swap
second.next = curr
curr.next = nxtPair
prev.next = second
# Update
prev = curr
curr = nxtPair
return dummy.nextTime Complexity
O(N)
Space Complexity
O(1)

