Given a linked list, swap every two adjacent nodes and return its head.

 
Example

Given 1->2->3->4, you should return the list as 2->1->4->3.

Challenge

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 成对交换节点的更多相关文章

  1. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  2. 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 ...

  3. [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 ...

  4. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

  5. [LeetCode]Swap Nodes in Pairs 成对交换

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  6. 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 ...

  7. 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 算法思想:基本操作就是链表 ...

  8. 【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 ...

  9. [LeetCode]Swap Nodes in Pairs题解

    Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...

随机推荐

  1. html、css、javascript、JSP 、xml学习顺序应该是怎样的呢?

    html==>css==>javascript==>jsp==>xml 1.先学习基本的HTML知识,了解大部分HTML标签的作用. 2.学习CSS,熟悉如何用CSS去控制HT ...

  2. Hadoop RPC机制的使用

    一.RPC基础概念 1.1 RPC的基础概念 RPC,即Remote Procdure Call,中文名:远程过程调用: (1)它允许一台计算机程序远程调用另外一台计算机的子程序,而不用去关心底层的网 ...

  3. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  4. MIT 6.828 JOS学习笔记2. Lab 1 Part 1.2: PC bootstrap

    Lab 1 Part 1: PC bootstrap 我们继续~ PC机的物理地址空间 这一节我们将深入的探究到底PC是如何启动的.首先我们看一下通常一个PC的物理地址空间是如何布局的:        ...

  5. 配置tomcat下war包可以自压缩

    <Host name="localhost" appBase="/home/hark/web" unpackWARs="true" a ...

  6. 自定义ActionBar

    /** * 1.创建ActionBar对象getSupportActionBar() * 2.布置自己的ActionBar布局(在res/layout) * 3.把自定义的ActionBar布局加载到 ...

  7. Nginx [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)

    使用命令关闭占用80端口的程序 sudo fuser -k 80/tcp

  8. AOP静态代理解析1-标签解析

    AOP静态代理使用示例见Spring的LoadTimeWeaver(代码织入) Instrumentation使用示例见java.lang.instrument使用 AOP的静态代理主要是在虚拟机启动 ...

  9. iphone 数字字段颜色兼容问题

    在iphone手机中,有些字段的颜色会自动发生变化,在head中添加如下meta即可.<meta name = "format-detection" content=&quo ...

  10. 不同java 版本的新功能

    Java 5 泛型 自动装箱/拆箱 增强的for 类型安全的枚举 可变参数 静态导入 Annotation Concurrent Package Java 6 Web Service 支持Annota ...