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: 交换相邻的两个节点 如上 ...
随机推荐
- 修改git上传代码的作者姓名
本文参考https://www.cnblogs.com/weiaiqi/p/11842515.html 随着IT行业的不断壮大,开源的东西越来越多,使用git来进行代码管理的人自然也越来越多,而且很多 ...
- 9-Pandas之数据合并与轴向连接(pd.concat()的详解)
数据合并:由于数据可能是不同的格式,且来自不同的数据源,为了方便之后的处理与加工,需要将不同的数据转换成一个DataFrame. Numpy中的concatenate().vstack().hstac ...
- Python os.chmod() 方法
概述 os.chmod() 方法用于更改文件或目录的权限.高佣联盟 www.cgewang.com 语法 chmod()方法语法格式如下: os.chmod(path, mode) 参数 path - ...
- Python File tell() 方法
概述 tell() 方法返回文件的当前位置,即文件指针当前位置.高佣联盟 www.cgewang.com 语法 tell() 方法语法如下: fileObject.tell() 参数 无 返回值 返回 ...
- PHP vsprintf() 函数
实例 把格式化字符串写入变量中: <?php高佣联盟 www.cgewang.com$number = 9;$str = "Beijing";$txt = vsprintf( ...
- PDOStatement::setAttribute
PDOStatement::setAttribute — 设置一个语句属性(PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)高佣联盟 www.cgewang.com 说 ...
- Qt 乱码
个人认识: 乱码的原因: 在编写代码时-->文件的格式--->编译器对文件进行编译的时候看到的只是二进制(乱码就出现在这里) 应合适方法 通知编译器(为什么说通知编译器呢?因为个人觉得这样 ...
- 一个C++版本的Sqlite3封装--SmartDb
Sqlite是一个非常轻量级的开源数据库,在嵌入式系统中使用的比较多,存储管理数据非常方便,Sqlite库提供的基于C语言的API,用起来也挺简单,但是有一点不太好的就是API使用起来有些繁琐,另外就 ...
- “随手记”开发记录day01
今天进行了第二次团队会议,并且开始了“随手记”APP的开发. 今天,我们的完成了登陆.注册页面,开始完成记账部分页面和个人信息页面. 完成页面如下:
- 题解 UVA10457
题目大意:另s = 路径上的最大边权减最小边权,求u到v上的一条路径,使其s最小,输出这个s. 很容易想到枚举最小边然后跑最小瓶颈路. so,如何跑最小瓶颈路? 利用Kruskal,因为树上两点路径唯 ...