题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值。

思路:迭代法和递归法都容易写,就写个递归的了。

4ms

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head || !head->next ) return head; ListNode *t=head->next;
head->next=swapPairs(t->next);
return t->next=head, t;
} };

AC代码

LeetCode Swap Nodes in Pairs 交换结点对(单链表)的更多相关文章

  1. [LeetCode]Swap Nodes in Pairs题解

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

  2. LeetCode: Swap Nodes in Pairs 解题报告

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

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

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

  4. 024 Swap Nodes in Pairs 交换相邻结点

    给定一个链表,对每两个相邻的结点作交换并返回头节点.例如:给定 1->2->3->4,你应该返回 2->1->4->3.你的算法应该只使用额外的常数空间.不要修改列 ...

  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 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

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

  7. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

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

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

  9. leetcode—Swap Nodes in Pairs

    1.题目描述 Given a linked list, swap every two adjacent nodes and return its head.   For example, Given ...

随机推荐

  1. java接口的方法默认都是public abstract类型

    java接口的方法默认都是public abstract类型, 因此我们在编写接口的实现类的时候,方法的实现前面要显式的加上public访问控制符

  2. HtmlAgilityPack 简单运用

    WebClient client = new WebClient(); MemoryStream ms = new MemoryStream(client.DownloadData("htt ...

  3. 如何解决VS启动越来越慢

    VS2013 用久后,现在启动和打开项目变得很慢 解决方案: A.清理缓存 VS2010清理缓存:启用vs2010命令行工具:在vs2010命令提示符下,执行devenv.exe /resetuser ...

  4. TCP/IP协议栈与数据包封装+TCP与UDP区别

    ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的TCP/IP协议栈获得了更为广泛的应用.如图2-1所示,是TCP/IP参考模型和OSI参考模型的对比示意图. T ...

  5. stringUtils是apache下的Java jar补充包

    org.apache.commons.lang.StringUtils StringUtils中一共有130多个方法,并且都是static的, 所以我们可以这样调用StringUtils.xxx().

  6. 如何发布使用LGPL版Qt的商业软件

    最近做跨平台图形用户界面库选型,权衡很多因素后最终选择了Qt,其中一个重要因素就是Qt使用LGPL授权许可.由于本人对LGPL理解有限,始终对闭源商业软件如何发布Qt库存在疑问,其中最关心的是:发布的 ...

  7. esriFeatureType与esriGeometryType的区别与联系

    esriFeatureType通常用来表示数据的存储结构,即物理层: esriGeometryType通常用来表示数据的几何形状,即表现层. esriGeometryType枚举类型详解 常量 值 对 ...

  8. Linux 修改计算机名

    查看计算机名:在终端输入hostname 修改的话 hostname +计算机名(重启后失效) 要永久修改的话要修改配置文件/etc/sysconfig/network 修改hostname=你要改的 ...

  9. 受限波兹曼机导论Introduction to Restricted Boltzmann Machines

    Suppose you ask a bunch of users to rate a set of movies on a 0-100 scale. In classical factor analy ...

  10. 量化Hacker News 中50天的数据 Quantifying Hacker News with 50 days of data

    Quantifying Hacker News I thought it would be fun to analyze the activity on one of my favorite sour ...