【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 ...
随机推荐
- docker启动:Got permission denied while trying to connect to the Docker daemon
权限问题: 1.查看所有用户组与用户 vim /etc/group /etc/group 的内容包括用户组(Group).用户组口令.GID及该用户组所包含的用户(User),每个用户组一条记 ...
- java常用技术名词解析
1.1 token Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便 将此Token返回给客户端,以后客户端只需带上这个Token前来请求数 ...
- IPC 之 ContentProvider 的使用
一.概述 ContentProvider 是 Android 中提供的专门用于不同应用间进行数据共享的方式.和 Messenger 一样,ContentProvider 的底层实现同样也是 Binde ...
- python 修改excel
操作描述:需要实现数据不断写入的功能,首先,在固定位置建立一个空白的xls文件:其次,每次产生的数据先判断该xls已有几列数据,后缀上去. 具体过程: 要保证具有三个包,是xlrd,xlwt,xlut ...
- 由table理解display:table-cell
转载自:https://segmentfault.com/a/1190000007007885 table标签(display:table) 1) table可设置宽高.margin.border.p ...
- Spark之standalone模式
standalone hdfs:namenode是主节点进程,datanode是从节点进程 yarn:resourcemanager是主节点进程,nodemanager是从节点进程 hdfs和yarn ...
- 学习笔记29—Linux基础
一.简单命令: 1.进入文件夹heyi目录:cd ./heyi 2.查看该目录下内容:ls 二.linux压缩和解压缩命令大全 tar命令 解包:tar zxvf FileName.tar 打包:ta ...
- d3 data()数据绑定中的key函数
官网https://github.com/d3/d3-selection/blob/master/README.md#selection_data var data = [ {name: " ...
- P10.3 usestock0.cpp
stock.h #ifndef STOCK_H #define STOCK_H #include <string> class Stock //类声明 { private: std::st ...
- Codeforces 985 F - Isomorphic Strings
F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...