while(curr.next != null && curr.next.next != null){
ListNode temp1 = curr.next;
ListNode temp2 = curr.next.next.next;
curr.next = curr.next.next;
curr.next.next = temp1;
temp1.next = temp2;
curr = curr.next.next;
}
对链表中每两个相邻节点进行交换的操作。这种操作通常用于链表中的节点交换,常用于链表的排序和反转等场景。
代码片段中的循环条件是 while (curr.next != null && curr.next.next != null)
,这表示只有在至少有两个节点的情况下才执行交换操作。在循环内部,创建了临时节点 temp1
和 temp2
,然后交换了当前节点 curr
和 curr.next
,以及它们的 next
指针。