Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
(交换链表中相邻的两个值)
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.
Example:
链表指针
这个题是一个很直观的指针问题,在设计算法的过程中将指针的变换在纸上设计出来即可得出算法,具体实现过程如下:
(引入头指针是一个链表中常用的方法)
1 | # Definition for singly-linked list. |