Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)
题目内容
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list’s nodes, only nodes itself may be changed.
大概意思是说每两个节点为一组交换位置。只使用常量空间,不能修改链表的值,只能修改链表的指针
解法(迭代)
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *dummy = new ListNode(-1), *pre = dummy;
dummy->next = head;
while (pre->next && pre->next->next) {
ListNode *t = pre->next->next;
pre->next->next = t->next;
t->next = pre->next;
pre->next = t;
pre = t->next;
}
return dummy->next;
}
};
该代码已经是非常经典了,但乍一看还是有点晕,我们来对代码进行一步步的解析。
可以看出该代码实现了两个相邻节点的交换,最后并将pre指向了下一个节点,然后继续该过程
递归解法
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode *t = head->next;
head->next = swapPairs(head->next->next);
t->next = head;
return t;
}
};
Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)的更多相关文章
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- [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 ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- 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 ...
- (链表 递归) 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交换节点对
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 (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
随机推荐
- SSM框架练习之Jsp页面使用taglib标签报错500的问题
最近在练手一个SSM的基于AdminLET框架模板的后台管理系统,使用的环境是tomcat9,使用Maven构建并通过添加Web模板框架的项目,在添加完所有的配置文件后启动tomcat运行,出现了一个 ...
- 00_01_使用Parallels Desktop创建WindosXP虚拟机
打开paralles软件,选择文件->新建 继续 选择手动选择,之后勾选没有指定源也继续 选择要创建的操作系统(这里以XP为例,其他的windows系统安装基本都差不多) 根据需要选择,这里选择 ...
- Python 数字数据类型
数字数据类型,包括整数.浮点数.复数和布尔类型. 整数 int 长整型(数字长度不限制):包括正数,负数,0. # 正数 num_int = 123 print(type(num_int)) # &l ...
- PHP krsort() 函数
------------恢复内容开始------------ 实例 对关联数组按照键名进行降序排序: <?php$age=array("Peter"=>"35 ...
- jzyz 题库 题目选做
题库中也有很多我想不出来的模拟赛的题目.做还是必要的.做自己的题目 时间很紧 想想自己的文化课 我又没有那么强 我必须得刷. LINK:水题一道 发现是一道计数题 计数题拿高分的才是王者,但是 计数题 ...
- AtCoder Grand Contest 044 A Pay to Win 贪心
LINK:Pay to Win 自闭了 比赛的时候推出来正解了 以为复杂度不对 写完扔了 没拿map存状态就扔了23333... 一个T点:在更新map的时候 >不要写成>= 不然会徒劳的 ...
- Linux的VMWare中Centos7文件权限管理chown 和 chmod
文件管理 chown chmod 1./根目录下目录功能划分 /boot/ 存放系统启动程序菜单及核心 --可以单独使用文件系统 /etc/ 存放系统中所有配置文件 /bin/ ...
- 执行HiveSQL出现的问题
-- ::, INFO [main] org.apache.hadoop.hive.ql.exec.ReduceSinkOperator: RECORDS_OUT_INTERMEDIATE:, -- ...
- CSS样式大全(网络收集整理)
CSS样式大全(网络收集整理 字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 { ...
- “随手记”APP与已经发布的记账软件“鲨鱼记账”的差距
我们使用并观察了“鲨鱼记账”APP,发现,我们的软件真的还有很多不足的地方.就功能这方面来说:“鲨鱼记账”APP有更多的收入.支出分类:就界面来说:“鲨鱼记账”APP有比我们优美太多的页面和背景.但是 ...