24. Swap Nodes in Pairs (Medium)
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4
, you should return the list as2->1->4->3
.Note:
- Your algorithm should use only constant extra space.
- You may not modify the values in the list's nodes, only nodes itself may be changed.
蠢办法用n来标记有没有一对,
n = 1时移动到一对中的第一个,记录一下pre
n = 2时 要开始swap了,注意有3个next要变,一个是node.next = pre,pre.next = node.next, ppre.next = node可以画图理解一下,ppre是这一对之前的node,pre是这一对里第一个node,node是这一对里第二个node