链表 24. 两两交换链表中的节点

public ListNode swapPairs(ListNode head) {
    ListNode h1 = head;
    if (head == null) {
        return null;
    }
<pre><code>if (head.next == null) {
    return head;
}

ListNode h2 = h1.next;
ListNode h3 = h2.next;

//特殊处理头节点
h2.next = h1;
h1.next = h3;
head = h2;

// 移动位置  h1 为h2前驱,h3为h2的前驱
h2 = h1.next;
if (h2 != null) {
    h3 = h2.next;
} else  {
    return head;
}

//交换h2,h3
while (h3 != null) {
    //删除h2
    h1.next = h3;
    //将h2插入h3后面
    h2.next = h3.next;
    h3.next = h2;

    h1 = h1.next;
    h1 = h1.next;
    h2 = h1.next;
    if (h2 != null) {
        h3 = h2.next;
    } else {
        break;
    }
}
return head;

}