当前位置:网站首页>LeetCode 92. Reverse Linked List II
LeetCode 92. Reverse Linked List II
2022-08-10 11:57:00 【Mizuna pen】
原题网址:https://leetcode.cn/problems/reverse-linked-list-ii/
给一个链表,Flip the nodes in an interval.
Find the previous node of the left interval,Then start deleting nodes,until out of range;Finally splicing the flipped linked list and the remaining linked list;
public ListNode reverseBetween(ListNode head, int left, int right) {
if(head == null || head.next == null || left == right) {
return head;
}
// 如果left是第一个数据,Unable to find its predecessor,So refer to the virtual node
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
// By finding the previous node,删除left节点
ListNode prev = null;
ListNode first = null;
ListNode tail = null;
ListNode cur = dummyHead;
for(int i=0;i<right;i++) {
// 找到left的前一个节点了,开始删除.
if(i>=left-1) {
prev = cur;
ListNode node = prev.next;
prev.next = node.next;
if(first == null) {
first = tail = node;
} else {
node.next = first;
first = node;
}
} else {
cur = cur.next;
}
}
tail.next = prev.next;
prev.next = first;
return dummyHead.next;
}
边栏推荐
- 负载均衡原理分析与源码解读
- 力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
- 苹果逆势扩大iPhone 14系列备货,总量或达9500万部
- LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
- [Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
- Analysis of the implementation principle of UUID from the perspective of source code
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- Not just running away, but saving the guy who mishandled rm -rf /*
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- Stroke Practice - 62 Valid Sudokus
猜你喜欢

即时零售业态下如何实现自动做账?

为什么Redis很快

How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize

再有人问你分布式事务,把这篇扔给他

电脑怎么设置屏幕息屏时间(日常使用分享)

StoneDB Document Bug Hunting Season 1

Article take you understand interrupt the key driver of polling mechanism

怎么加入自媒体,了解这5种变现模式,让账号快速变现

网络基础(第一节)

AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
随机推荐
微信小程序提交审核历史版本记录从哪里查看
Licking Exercise - 60 Maximum key-value sum of binary search subtrees
彩色图和深度图转点云
传三星3nm斩获第二家客户,目前产能已供不应求
rider内Mono脚本找不到引用资源
老板加薪!看我做的WPF Loading!!!
有哪些好用的性能测试工具推荐?性能测试报告收费标准
LAXCUS分布式操作系统安全管理
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
Network Fundamentals (Section 1)
为什么Redis很快
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
振弦传感器及核心VM系列振弦采集模块
【Untitled】
基于UiAutomator2+PageObject模式开展APP自动化测试实战
力扣练习——64 最长和谐子序列
Redis常用命令
即时零售业态下如何实现自动做账?
快速上手,征服三种不同分布式架构调用方案