【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 ...
随机推荐
- 51nod 1020 逆序排列
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1020 题意: 思路: 一开始用了三重循环... 设f(n,k)表示n个数 ...
- Spring线程池
•依赖 spring-context-support •配置: <bean id ="taskExecutor" class ="org.springframewo ...
- centos7 安装nexus3
一.安装前先安装好java JDK 和maven nexus 下载 链接:https://pan.baidu.com/s/1qQBNj2soc8Un4AoRejvEyw 密码: sb12 1.下载好后 ...
- JavaSE 字符串和正则表达式
根据不懂的自己整理一下,跟着老师进度刷一遍课本,记录琐碎不懂知识 1.StringTokenizer类 String[] mess= {"整数部分","小数部分" ...
- Hadoop如何将TB级大文件的上传性能优化上百倍?
这篇文章,我们来看看,Hadoop的HDFS分布式文件系统的文件上传的性能优化. 首先,我们还是通过一张图来回顾一下文件上传的大概的原理. 由上图所示,文件上传的原理,其实说出来也简单. 比如有个TB ...
- openvswitch vlan下的虚拟机与物理机通信
1,安装openvswitch ,图形界面显示等用到的安装包. yum install libvirt openvswitch python-virtinst xauth tigervnc -y 2, ...
- 类似于placehoder效果的图标展示
在做app开发的时候往往会有那个注册登录啊,什么的页面,里面就会包含这那种类似于placeholder的效果的图标,当时我也是和ios和安卓混合开发一款app里面的页面全是我写,最开始就是登陆啊,注册 ...
- Pandas存储为Excel格式:单个xlsx文件下多sheet存储方法
Notes If passing an existing ExcelWriter object, then the sheet will be added to the existing workbo ...
- Python全栈开发-Day11-RabbitMQ/Redis
本节内容 RabbitMQ——消息队列 Memcached & Redis使用 1.RabbitMQ——消息队列 RabbitMQ与Queue的关系 1.做的事情是一样的,两者都是队列. 2. ...
- R语言函数总结(转)
R语言特征 对大小写敏感 通常,数字,字母,. 和 _都是允许的(在一些国家还包括重音字母).不过,一个命名必须以 . 或者字母开头,并且如果以 . 开头,第二个字符不允许是数字. 基本命令要么是表达 ...