[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 ... 
随机推荐
- Linux---从start_kernel到init进程启动
			“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” ini ... 
- 彻底删除Kafka中的topic
			1.删除kafka存储目录(server.properties文件log.dirs配置,默认为"/tmp/kafka-logs")相关topic目录 2.Kafka 删除topic ... 
- DampView阻尼效果
			阻尼效果即是图片向下拉动时会放大,松开会回弹 1.自定义一个DampView类,继承ScrollView 2.布局最外层必须是DampView,且DampView和要拉动的图片之间只能有一层layou ... 
- LoadRunner脚本实例来验证参数化的取值
			LoadRunner脚本实例来验证参数化的取值 SINM {3]!G0问题提出: 主要想试验下,在Controller中,多个用户,多次迭代中参数的取值.51Testing软件测试网(['H5f,d ... 
- SpringHttpInvoker解析1-使用示例
			HTTP invoker是一个新的远程调用模型,作为Spring框架的一部分,来执行基于HTTP的远程调用(让防火墙可以接受),并使用Java的序列化机制. 服务端 定义服务接口UserService ... 
- 基于netty的微服务架构
			基于netty的微服务架构 微服务一篇好文章 http://san-yun.iteye.com/blog/1693759 教程 http://udn.yyuap.com/doc/essential-n ... 
- String equals()方法使用以及子串加密
			String equals()方法的实现方法: 名称 说明 String.Equals (Object) 确定此 String 实例是否与指定的对象(也必须是 String)具有相同的值. Strin ... 
- java 常见下载合集
			J2SE SDK (JDK): Windows: http://download.oracle.com/auth/otn-pub/java/jdk/6u25-b06/jdk-6u25-windows- ... 
- 学习 Message(5): 关于 TApplicationEvents.OnMessage 的第二个参数  可以屏蔽 TWebBrowser右键菜单:
			http://www.cnblogs.com/del/archive/2008/10/25/1319318.html TApplicationEvents.OnMessage 的第二个参数 Handl ... 
- list操作 foreach和for的区别
			foreach只是简单的遍历读取,不能在循环中进行remove等操作. for可以 
