当前位置:网站首页>leetcode 92. Reverse linked list II
leetcode 92. Reverse linked list II
2022-07-22 13:10:00 【Blue feather birds】
Not the whole list in reverse order , Is to take a paragraph , stay left ~ right The linked list of this range is in reverse order .
Ideas :
First, clarify how to reverse the order ,
Because it's a linked list , So numbers are not just exchange , It can be inserted in front .
Take the above example ,2->3->4 The reverse order of becomes 4->3->2
hold 2 The following numbers are inserted in turn to the front , That is, complete the reverse order :
3->2->4, 4->3->2
Then how to judge which is the front , So you need a pre node , Point to left Previous node ,
Then if left yes head What about nodes ,
You need one newHead, newHead.next = head,
It's also convenient that we don't have to change back and forth head node .
public ListNode reverseBetween(ListNode head, int left, int right) {
if(left == right) return head;
ListNode newHead = new ListNode();
newHead.next = head;
ListNode pre = newHead;
for(int i = 0; i < left-1; i++) pre = pre.next;
ListNode cur = pre.next;
for(int i = left; i < right; i ++) {
ListNode tmp = cur.next;
cur.next = tmp.next;
tmp.next = pre.next;
pre.next = tmp;
}
return newHead.next;
}
边栏推荐
- el-input 失去焦点事件
- ECCV 2022 | generalized long tail classification based on invariant feature learning
- mapMutations在项目中的使用
- 三种JS截取字符串方法
- GetType returned an empty solution due to namespace problems
- IOOutputStream类型
- Wechat hotel reservation applet graduation project (4) opening report
- 系统架构设计师备考经验分享:保障复习时间
- [higher number] prove by definition that the limit of 1/n power of a is 1
- 4种 Redis 集群方案及优缺点对比
猜你喜欢
Unlock the correct combination method of CNN and transformer, and ByteDance proposes an effective next-generation visual transformer
华为无线设备配置攻击检测功能
:class在项目中的使用
LEGO loam run through and source code learning
机器学习-集成学习
How to select the type of ankerui intelligent miniature circuit breaker?
The graduation design of wechat hotel reservation applet (3) background function
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记2:分析与可解释性
97页数字孪生赋能的智慧园区物联网云平台建设方案
《6》 BFC
随机推荐
4种 Redis 集群方案及优缺点对比
华为无线设备配置WIDS和WIPS
Multiple backpack notes
Application of international standard iso/iec 30144: 2020 in auxiliary monitoring of Intelligent Substation
: use of class in the project
"Xiaodeng in operation and maintenance" ensures gdpr compliance by auditing user activities and internal changes in the ad domain
Concept, architecture and key technologies of industrial Internet
Esp8266 analog input (ADC) detection problem
Force buckle to distribute biscuits
点绕网格中心顺时针旋转 90° 180° 270°坐标的变化
[pan micro ecology] compiling_ jsp/_ login/_ upgrademessage...
Matlab digital image processing homework: facial expression recognition
超干货!彻底搞懂单工、半双工、全双工的区别与联系
B-end product manager learning: import template design
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
如何使用多类型数据预训练多模态模型?
【技术】uniapp 之引入 u-charts 部分demo
智能仪器仪表行业数字化供应链管理系统:加速企业智慧供应链平台转型
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记1:推荐系统
[ kitex 源码解读 ] Kitex 扩展性设计思路