. - 力扣(LeetCode)
一、题目:
二、模拟
1. 第一步
2. 第二步:current = next.next
3. 第三步: next.next = current
4. 第四步:pre.next = next;
到这里为止实现了两个节点的交换
5. 第五步:pre = current; current = current.next;
三、完整代码
ListNode dummy = new ListNode(-1);
dummy.next = head;ListNode current = head;
ListNode pre = dummy;while(current != null && current.next != null){ListNode next = current.next;current = next.next;next.next = current;pre.next = next;pre = current;current = current.next;
}return dummy.next;