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. HDU 4320 Arcane Numbers 1(质因子包含)

    http://acm.hdu.edu.cn/showproblem.php?pid=4320 题意: 给出A,B,判断在A进制下的有限小数能否转换成B进制下的有限小数. 思路: 这位博主讲得挺不错的h ...

  2. 创建react项目

    npm搭建React项目 React官网提供最简便的方法是使用create-react-app npx create-react-app my-app cd my-app npm start 也可以自 ...

  3. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  4. python (协程)生产者,消费者

    #coding=utf- import gevent from gevent.queue import Queue, Empty import time tasks = Queue(maxsize=) ...

  5. mongodb,Mysql,redis基础教程

    数据库基础 1:mongodb基础教程 1:pymongo基础教程  2:Mysql基础教程 3:redis基础教程

  6. 读书笔记《Spring Boot实战 —— Java EE 开发的颠覆者》

    Spring框架是轻量级的企业级开发一站式解决方案 Spring使用简单的POJO Plain Old Java Object 无限制的普通Java对象 Spring Framework Runtim ...

  7. 力扣(LeetCode)1.两数之和

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元 ...

  8. PSFTP用法

    PSFTP是PuTTY SFTP客户端,用于本地与服务器间安全传输文件(使用SSH连接). 1. 启动PSFTP 在Windows命令提示符中输入 set PATH=C:/PSFTP.exe所在路径; ...

  9. Codeforces 960F - Pathwalks

    960F - Pathwalks 思路: ORZ 杜老师 用map写1e5个树状数组,骚操作 记Q为query和update次数,则节点个数约为Q*log(N) 代码: #include<bit ...

  10. Unity中sharedMaterials 和 materials

    sharedMaterials 和 materials: 这两个属性用法是一样的,但是从效率上来说最好用sharedMaterial,它是共享材质,无论如何操作材质的属性(如更换颜色或者更换shade ...