[LeetCode]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.
这道题我尝试写了用两个指针交换的方法,结果Runtime error了。
最后看了Discuss的解答,整理一下思路。首先,用指针的指针pp指向头部,a和b分别指向当前的node节点的指针和指向下一个节点的指针。而我们要做到的是把 *pp == a -> b -> (b->next) 转换成 *pp == b -> a -> (b->next)。事实上代码中while循环内前三行做的就是这个任务。
代码如下:
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode **pp = &head, *a, *b;
while ((a = *pp) && (b = a->next)) {
a->next = b->next;
b->next = a;
*pp = b;
pp = &(a->next);
}
return head;
}
};
[LeetCode]Swap Nodes in Pairs题解的更多相关文章
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
随机推荐
- Linux中一些 不是很常用的配置修改
1,让虚拟机屏幕最大化 :查看-->自动调整大小-->自动适应客户机 2,让虚拟机取消屏保: system --> preferences --> Screensaver
- elasticsearch-analysis-pinyin
来源:https://github.com/medcl/elasticsearch-analysis-pinyin Pinyin Analysis for Elasticsearch This Pin ...
- scrapy实战2,使用内置的xpath,re和css提取值
以伯乐在线文章为爬取目标blog.jobbole.com,发现在"最新文章"选项中可看到所有文章 一般来说,可以用scrapy中自带的xpath或者css来提取数据,定义在 ...
- 对 ArrayList 进行分页.
/** * 测试分页 */ @Test public void testPage() { int bulkSize = 2; List<Integer> dataList = new Ar ...
- 大数据-HBase HA集群搭建
1.下载对应版本的Hbase,在我们搭建的集群环境中选用的是hbase-1.4.6 将下载完成的hbase压缩包放到对应的目录下,此处我们的目录为/opt/workspace/ 2.对已经有的压缩包进 ...
- 05-树9 Huffman Codes (30 分)
In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...
- compact framework windows mobile wm c#代码 创建快捷方式
已经2018年了,windows mobile已经宣布不维护狠多年了,不要问我为什么还在开发windows mobile的程序,我也不想.公司有一批手持扫描枪设备依然是windows mobile的程 ...
- Sublime Text 3安装插件(Mac 10.12)
1.先安装Package Control,默认这个是没有安装的. 使用[control + -]打开控制台,输入以下代码: import urllib.request,os; pf = 'Packag ...
- React之表单
第一部分:表单基础 在React中,修改表单的唯一途径是使用setState方法.举例如下: class NameForm extends React.Component { constructor( ...
- redis 数据持久化 aof方式
redis持久化-Append-only file(缩写aof)的方式 本质:把用户执行的每个 ”写“ 指令(增加.修改.删除)都备份到文件中,还原数据的时候就是执行具体写指令. 打开redis的运 ...