题目链接
https://leetcode.com/problems/swap-nodes-in-pairs/
题目原文
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
题目翻译
给定一个单链表,交换相邻的节点,并返回链表头。比如,给定1->2->3->4,你应该返回 2->1->4->3。
注意:你的算法应该是O(1)的空间复杂度,不能修改链表节点的值(val),只能修改节点。
思路方法
思路一
既然每次交换相邻节点,那么就以两个节点为一个单位做处理,循环每次交换两个相邻节点即可。
代码