> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/04-18-2022-24.-swap-nodes-in-pairs.md).

# 04/18/2022 24. Swap Nodes in Pairs

```
// Some code
class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null) return null;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode curr = head;
        
        while ((curr != null) && (curr.next != null)){// if there is only one node. no need to swap.
            pre.next = curr.next;
            ListNode tmp = curr.next.next;
            curr.next.next = curr;
            curr.next = tmp;
            
            pre = curr;
            curr = curr.next;
        }
        return dummy.next;
    }
}
```

//first and second pointers&#x20;

```
// Some code
class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null) return null;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode curr = head;
        
        while ((curr != null) && (curr.next != null)){
            ListNode first = curr;
            ListNode second = curr.next;
            
            
            pre.next = second;
            first.next = second.next;
            second.next = first;
            
            pre = curr;
            curr = curr.next;
        }
        return dummy.next;
    }
}
```
