当前位置:网站首页>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;
}
边栏推荐
- Introduction to Software Architecture
- [Go WebSocket] 多房间的聊天室(一)思考篇
- 阻塞 非阻塞 poll机制 异步
- 【LeetCode】640. 求解方程
- 使用哈工大LTP测试分词并且增加自定义字典
- Chapter 22 Source Code File REST API Reference (4)
- 【勇敢饭饭,不怕刷题之链表】有序链表的合并
- Open Office XML 格式里如何描述多段具有不同字体设置的段落
- Licking Exercise - 60 Maximum key-value sum of binary search subtrees
- 3款不同类型的自媒体免费工具,有效提高创作、运营效率
猜你喜欢
网络套接字(UDP和TCP编程)
一文带你搞懂中断按键驱动程序之poll机制
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
Nocalhost - 让云原生时代的开发更高效
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
电脑怎么设置屏幕息屏时间(日常使用分享)
Some tips for using Unsafe
Analysis of the implementation principle of UUID from the perspective of source code
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
Emulate stm32 directly with proteus - the programmer can be completely discarded
随机推荐
Double.doubleToLongBits() method uses
2022年裁员潮,失业程序员何去何从?
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
Some tips for using Unsafe
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
从源码角度分析UUID的实现原理
使用JMeter进行MySQL的压力测试
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
OPNsense安装配置Zenarmor
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
使用.NET简单实现一个Redis的高性能克隆版(六)
实现内网穿透的最佳解决方案(无实名认证,完全免费)
Redis常用命令
振弦传感器及核心VM系列振弦采集模块
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
负载均衡原理分析与源码解读
【勇敢饭饭,不怕刷题之链表】有序链表的合并
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
CPU多级缓存与缓存一致性
codevs 2370 小机房的树 (LCA)