题目摘要
交换链表相邻两个节点的顺序,返回头结点。1->2->3->4
变成2->1->4->3
解法
从头开始两两交换,注意需要保留两个节点的前一个节点才能顺利交换,保证列表不断。
注意
原题
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.