dummy
as a new ListNode that points to head
.prevNode
to dummy
.firstNode
and firstNode.next
(which will be secondNode
) are not null:
a. Save firstNode.next
in secondNode
.
b. Perform the swap by updating firstNode.next
to secondNode.next
and secondNode.next
to firstNode
.
c. Update prevNode.next
to secondNode
to maintain the list's integrity.
d. Move prevNode
to firstNode
.
e. Update firstNode
to firstNode.next
(the next pair's first node).dummy.next
, which is the new head of the swapped list.