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

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

 ListNode* swapPairs(ListNode* head) {
ListNode *p = head, *q, *pre;
while (p) {
if (q = p->next) {
if (p == head) head = q;
p->next = q->next;
q->next = p;
if (pre) pre->next = q;
}
pre = p;
p = p->next;
}
return head;
}

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

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

 ListNode* swapPairs(ListNode* head) {
ListNode **pp = &head, *p, *q;
while ((p = *pp) && (q = p->next)) {
p->next = q->next;
q->next = p;
*pp = q;
pp = &(p->next);
}
return head;
}

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

 ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode *p = head, *q = p->next;
if (!q->next)
p->next = NULL;
else
p->next = swapPairs(q->next);
q->next = p;
return q;
}
 ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode **pp = &head, *q = (*pp)->next;
(*pp)->next = swapPairs(q->next);
q->next = *pp;
pp = &q;
return *pp;
}

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

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

 ListNode* swapPairs(ListNode* head) {
if(!head || !head->next) return head;
ListNode *next = head->next;
head->next = swapPairs(next->next);
next->next = head;
return next;
}

【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. Linux安装Broadcom无线驱动

    参考https://blog.csdn.net/u012833250/article/details/52493806 首先查看自己的网卡型号,然后先执行sudo apt-get update 再根据 ...

  2. hihoCoder 1515 分数调查(带权并查集)

    http://hihocoder.com/problemset/problem/1515 题意: 思路: 带权并查集的简单题,计算的时候利用向量法则即可. #include<iostream&g ...

  3. linux 进阶命令笔记(12月26日)

    1. df 指令 作用:查看磁盘空间 用法: #df -h       -h 表示以可读性较高的形式展示大小   2.free 指令 作用:查看内存使用情况 语法:#free -m       -m表 ...

  4. 如何判断一个js对象是否是Array

    经常遇到一个问题,判断某个对象是否为数组类型,在Js中检测对象类型的常见方式有以下: typeof操作符 对于Function.String.Number.Undefined等几种类型的对象来说,几乎 ...

  5. Python 汉诺塔游戏

    #n 多少个盘子 def hanoi(n,x,y,z): : print(x,'→',z) else: hanoi(n-, x, z,y) #将前n-1个盘子从X移动到y上 print(x,'→',z ...

  6. 【四】php 函数

    一:函数 作用:为了分割那些能够独立完成有明确任务的代码,更易于阅读 调用函数:function_name(args1,args2...); args可以是任何一种php变量,包括数组或对象 函数名不 ...

  7. nodejs项目安装ant design

    1.确保安装好nodejs $ node --version v10.4.1 2.确保npm $ npm -v 6.1.0 3.安装 $ sudo npm install antd-init -g / ...

  8. eclipse maven maven-compiler-plugin 报错 完全解决

    报错如下: Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:comp ...

  9. 使用pymongo连接mongodb时报错:pymongo.errors.OperationFailure: not authorized

    连接本机或局域网部署的mongodb时可以用以下方法: from urllib import parse from pymongo import MongoClient host = '*.*.*.* ...

  10. prometheus的agent 二次开发代码参考

    import com.codahale.metrics.MetricRegistry;import io.prometheus.client.CollectorRegistry;import io.p ...