题目描述
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
示例:
给定 1->2->3->4, 你应该返回 2->1->4->3.
说明:
你的算法只能使用常数的额外空间。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
题解
两个注意点:
插入一个 dummy 头节点
注意在交换过程中不要动 first 和 second
public ListNode SwapPairs(ListNode head){var dummy = new ListNode(0) { next = head };var current = dummy;while (current.next != null && current.next.next != null){var first = current.next;var second = current.next.next;first.next = second.next;current.next = second;current.next.next = first;current = current.next.next;}return dummy.next;}
