当前位置:网站首页>LeetCode 24. 两两交换链表中的节点
LeetCode 24. 两两交换链表中的节点
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/swap-nodes-in-pairs/submissions/
关于交换的问题;https://blog.csdn.net/qq_34501351/article/details/126203341?spm=1001.2014.3001.5501 这题也是交换的问题;由于可以取到交换的目标节点;引入一个虚拟头结点;可以更好的得到目标节点的前一个节点;更容易;
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode cur = dummyHead;
ListNode node = head;
while(node != null) {
ListNode frist = node;
ListNode second = node.next;
// 只有一个数,不用换了,返回结果
if(second == null) {
return dummyHead.next;
}
frist.next = second.next;
second.next = frist;
cur.next = second;
cur = frist;
node = frist.next;
}
return dummyHead.next;
}
边栏推荐
- OPNsense安装配置Zenarmor
- mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
- VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
- Double.doubleToLongBits()方法使用
- POJ 3101 Astronomy (Mathematics)
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- 力扣练习——63 找到字符串中所有字母异位词
- 程序员追求技术夯实基础学习路线建议
- 蔚来-软件开发工程师一面记录
- 老板加薪!看我做的WPF Loading!!!
猜你喜欢
随机推荐
基于UiAutomator2+PageObject模式开展APP自动化测试实战
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
从产品维度来看 我们为什么不能完全信任Layer2?
Licking Exercise - 63 Find all anagrams in a string
Spss-多元回归案例实操
越折腾越好用的 3 款开源 APP
暑期总结4
Redis常用命令
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
Licking Exercise - 60 Maximum key-value sum of binary search subtrees
Centos7 environment uses Mysql offline installation package to install Mysql5.7
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
10 个 Reduce 常用“奇技淫巧”
从源码角度分析UUID的实现原理
Nocalhost - Making development more efficient in the cloud-native era
flask-restplus接口地址404问题
单目操作符(含原码反码补码转换)


![[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists](/img/06/9d49fc99ab684f03740deb2abc38e2.png)






