[LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head.
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.
LeetCode上的原题,请参见我之前的博客Swap Nodes in Pairs。
解法一:
class Solution {
public:
/**
* @param head a ListNode
* @return a ListNode
*/
ListNode* swapPairs(ListNode* head) {
ListNode *dummy = new ListNode(-), *pre = dummy;
dummy->next = head;
while (pre->next && pre->next->next) {
ListNode *t = pre->next;
pre->next = t->next;
t->next = t->next->next;
pre->next->next = t;
pre = t;
}
return dummy->next;
}
};
解法二:
class Solution {
public:
/**
* @param head a ListNode
* @return a ListNode
*/
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode *t = head->next;
head->next = swapPairs(head->next->next);
t->next = head;
return t;
}
};
[LintCode] Swap Nodes in Pairs 成对交换节点的更多相关文章
- [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 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- [LeetCode] 24. Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- [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 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
随机推荐
- JDK中的设计模式
Creational(创建模式) Abstract factory: 创建一组有关联的对象实例.这个模式在JDK中也是相当的常见,还有很多的framework例如Spring.我们很容易找到这样的实例 ...
- WINDOWS系统Eclipse+NDK+Android + OpenCv
WINDOWS系统Eclipse+NDK+Android + OpenCv 参考文档博客 1 NDK环境搭建 http://jingyan.baidu.com/article/5d6edee22d90 ...
- SecureCRT上传和下载文件(下载默认目录)
SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由于使 ...
- Android自动化压力测试之Monkey Test (三)
Monkey 是什么? Monkey是Google提供的一个用于稳定性与压力测试的命令行工具. Monkey路径: 路径:/System/framework/monkey.jar 启动脚本路径:/sy ...
- 【Android开发日记】Popupwindow 完美demo
Popupwindow 完美demo实现 图示: 关键代码说明: 1.弹出popupwindow,背景变暗 ColorDrawable cd = new ColorDrawable(0x000000) ...
- 移动终端app测试点总结
以下所有测试最后必须在真机上完整的执行1.安装.卸载测试 在真机上的以及通过91等第三方的安装与卸载 安装在手机上还是sd卡上 2.启动app测试3.升级测试 数字签名.升级覆盖安装.下载后手动覆盖安 ...
- 经典的nav导航
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- iOS学习01C语言数据类型
1.注释 // 单行注释 // 注释对代码起到解释说明的作用,注释是给程序员看的,不参与程序运行 /* 多行注释 Xcode快捷键 全选 cmd+a 复制 cmd+c 粘贴 cmd+v 设 ...
- django 安装
git clone https://github.com/django/django.git 或者到django的官网下载 然后 python setup.py install
- flexbox布局的兼容性
http://ayqy.net/blog/flexbox布局的兼容性/ 写在前面 flex布局早在2009年就有了,而现在是2015年6月8日,使用最新的flex语法会发现支持程度并不好,即使是在“高 ...