e.g. 给定链表 1->2->3->4,返回 2->1->4->3 的头节点。

我写了个常见的从头节点遍历,少量的奇数个或偶数个数据都能成功重新排列。但链表过长时结果显示 Time Limit Exceeded 。

  1. ListNode* swapPairs(ListNode* head) {
  2. ListNode *p = head, *q, *pre;
  3. while (p) {
  4. if (q = p->next) {
  5. if (p == head) head = q;
  6. p->next = q->next;
  7. q->next = p;
  8. if (pre) pre->next = q;
  9. }
  10. pre = p;
  11. p = p->next;
  12. }
  13. return head;
  14. }

看到有一个答案使用二级指针,忘了这种方法了!

pp 从指向 head 指针到指向所有 next 指针遍历下去,p 指向 *pp 表示指向当前结点,q 指向 p 的下一个节点。

  1. ListNode* swapPairs(ListNode* head) {
  2. ListNode **pp = &head, *p, *q;
  3. while ((p = *pp) && (q = p->next)) {
  4. p->next = q->next;
  5. q->next = p;
  6. *pp = q;
  7. pp = &(p->next);
  8. }
  9. return head;
  10. }

看到有些答案标题写使用递归,想了下,把两种思路都 DIY 为递归方法,Accepted了。

  1. ListNode* swapPairs(ListNode* head) {
  2. if (!head || !head->next) return head;
  3. ListNode *p = head, *q = p->next;
  4. if (!q->next)
  5. p->next = NULL;
  6. else
  7. p->next = swapPairs(q->next);
  8. q->next = p;
  9. return q;
  10. }
  1. ListNode* swapPairs(ListNode* head) {
  2. if (!head || !head->next) return head;
  3. ListNode **pp = &head, *q = (*pp)->next;
  4. (*pp)->next = swapPairs(q->next);
  5. q->next = *pp;
  6. pp = &q;
  7. return *pp;
  8. }

可以看到二级指针代码量确实会少一点,而且使用递归代替迭代有时能减少时间。

但答案这种递归方法最为简单,只用定义一个指针。改为使用二级指针目测无法实现。

  1. ListNode* swapPairs(ListNode* head) {
  2. if(!head || !head->next) return head;
  3. ListNode *next = head->next;
  4. head->next = swapPairs(next->next);
  5. next->next = head;
  6. return next;
  7. }

【LeetCode】成对交换节点的更多相关文章

  1. LeetCode OJ: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] Swap Nodes in Pairs 成对交换节点

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

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

  5. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  6. 我要好offer之 链表大总结

    单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNo ...

  7. 爬虫_python3_requests

    Requests 网络资源(URLs)撷取套件 改善Urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT,GET,DELETE)存取网络资源 import ...

  8. [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  9. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. 51nod 1020 逆序排列

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1020 题意: 思路: 一开始用了三重循环... 设f(n,k)表示n个数 ...

  2. Spring线程池

    •依赖 spring-context-support •配置: <bean id ="taskExecutor" class ="org.springframewo ...

  3. centos7 安装nexus3

    一.安装前先安装好java JDK 和maven nexus 下载 链接:https://pan.baidu.com/s/1qQBNj2soc8Un4AoRejvEyw 密码: sb12 1.下载好后 ...

  4. JavaSE 字符串和正则表达式

    根据不懂的自己整理一下,跟着老师进度刷一遍课本,记录琐碎不懂知识 1.StringTokenizer类 String[] mess= {"整数部分","小数部分" ...

  5. Hadoop如何将TB级大文件的上传性能优化上百倍?

    这篇文章,我们来看看,Hadoop的HDFS分布式文件系统的文件上传的性能优化. 首先,我们还是通过一张图来回顾一下文件上传的大概的原理. 由上图所示,文件上传的原理,其实说出来也简单. 比如有个TB ...

  6. openvswitch vlan下的虚拟机与物理机通信

    1,安装openvswitch ,图形界面显示等用到的安装包. yum install libvirt openvswitch python-virtinst xauth tigervnc -y 2, ...

  7. 类似于placehoder效果的图标展示

    在做app开发的时候往往会有那个注册登录啊,什么的页面,里面就会包含这那种类似于placeholder的效果的图标,当时我也是和ios和安卓混合开发一款app里面的页面全是我写,最开始就是登陆啊,注册 ...

  8. Pandas存储为Excel格式:单个xlsx文件下多sheet存储方法

    Notes If passing an existing ExcelWriter object, then the sheet will be added to the existing workbo ...

  9. Python全栈开发-Day11-RabbitMQ/Redis

    本节内容 RabbitMQ——消息队列 Memcached & Redis使用 1.RabbitMQ——消息队列 RabbitMQ与Queue的关系 1.做的事情是一样的,两者都是队列. 2. ...

  10. R语言函数总结(转)

    R语言特征 对大小写敏感 通常,数字,字母,. 和 _都是允许的(在一些国家还包括重音字母).不过,一个命名必须以 . 或者字母开头,并且如果以 . 开头,第二个字符不允许是数字. 基本命令要么是表达 ...