1public ListNode swapPairs(ListNode head) {
2 ListNode dummy = new ListNode(0);
3 dummy.next = head;
4 ListNode prev = dummy;
5
6 while (prev.next != null && prev.next.next != null) {
7 ListNode first = prev.next;
8 ListNode second = first.next;
9
10 // Swap the pair
11 prev.next = second;
12 first.next = second.next;
13 second.next = first;
14
15 // Move to next pair
16 prev = first;
17 }
18
19 return dummy.next;
20}