【LeetCode】成对交换节点
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】成对交换节点的更多相关文章
- 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 ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- [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 ...
- 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 ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 我要好offer之 链表大总结
单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNo ...
- 爬虫_python3_requests
Requests 网络资源(URLs)撷取套件 改善Urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT,GET,DELETE)存取网络资源 import ...
- [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 ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
随机推荐
- 在WPF中调用另存为对话框
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "User ...
- 1、HA Cluster基础原理
Linux Cluster --> linux集群类型分三种: LB:负载均衡,LoadBalance HA:双机集群系统,指高可用性集群,High Available HP:Hadoop ...
- 2017-2018-2 20165306 实验三《敏捷开发与XP实践》实验报告
实验三<敏捷开发与XP实践>实验报告 实验报告封面 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一) 敏捷开发与XP实践-1 实验要求: 参考 代码规范 安装alibaba 插 ...
- ZOJ 3987 Numbers(Java枚举)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3987 题意:给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n ...
- C++类模板和模板类
C++ 中有一个重要特性,那就是模板类型.类似于Objective-C中的泛型.C++通过类模板来实现泛型支持. 1 基础的类模板 类模板,可以定义相同的操作,拥有不同数据类型的成员属性. 通常使用t ...
- 【BZOJ】3139: [Hnoi2013]比赛
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3139 可以发现,答案之和得分的序列有关,而且和序列中每个元素的顺序无关.考虑HASH所有的 ...
- javaSE习题 第一章 JAVA语言概述
转眼就开学了,正式在学校学习SE部分,由于暑假放视频过了一遍,略感觉轻松,今天开始,博客将会记录我的课本习题,主要以文字和代码的形式展现,一是把SE基础加强一下,二是课本中有很多知识是视频中没有的,做 ...
- [原][osg][osgearth]倾斜摄影1.介绍
总体介绍: 倾斜摄影就是将拍好的数据,三角网格化再附上贴图. 目前流行处理软件: Street Factory.PIX4DMapper.smart3D 后期开发平台:超图 Skyline smart3 ...
- Linux 中 MySQL常用命令
一. 数据库登录mysql -uroot -p二..退出数据库quit 和 exit或ctrl + d三.数据库操作1. 查看所有数据库 show databases;2. 查看当前使用的数据库sel ...
- CURLE_OPERATION_TIMEDOUT libcurl 错误码28– 操作超时
在多线程情况下出现错误码28 是因为没有调用全局初始化函数 static int GlobleInit();//全局初始化,主程序调用一次,只能一次 static void GlobleFint(); ...