当前位置:网站首页>Specified interval inversion in the linked list
Specified interval inversion in the linked list
2022-07-03 06:59:00 【W_ Meng_ H】
Topic link :https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
analysis :
The original list :m = 2 ,n = 4
After reversing
1、 Need to get to m Start node
2、 Flip m - n The node of
3、m Node next Pointer to n Node next
4、m−1 Node next Pointer to n node
5、 If m=1, At this time ,m-1 Nodes don't exist , Add an empty node before the head node of the linked list to point to the head node
Code :
import java.util.*;
public class ListNode {
int val;
ListNode next = null;
}
public class Solution {
/**
*
* @param head ListNode class
* @param m int integer
* @param n int integer
* @return ListNode class
*/
public ListNode reverseBetween (ListNode head, int m, int n) {
if(m==n){
return head;
}
// write code here
ListNode d = new ListNode(-1);
d.next = head;
ListNode pre = d;// Precursor node to flip
for(int i=1;i<m;i++){
pre = pre.next;
}
head = pre.next;//m head
ListNode next; // For transit
//m-n Flip
for(int i=m; i<n;i++){
next = head.next;
head.next = next.next;
next.next = pre.next;
pre.next = next;
}
return d.next;
}
}
边栏推荐
- Personally design a highly concurrent seckill system
- Modify MySQL password
- My 2020 summary "don't love the past, indulge in moving forward"
- Win 10 find the port and close the port
- Jenkins
- Summary of UI module design and practical application of agent mode
- Asynchronous programming: async/await in asp Net
- Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
- [day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
- 【code】偶尔取值、判空、查表、验证等
猜你喜欢
EasyExcel
Setting up the development environment of dataworks custom function
Jenkins
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
Journal quotidien des questions (11)
Summary of UI module design and practical application of agent mode
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
随机推荐
instanceof
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
DBNet:具有可微分二值化的实时场景文本检测
利用C#实现Pdf转图片
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
【code】偶尔取值、判空、查表、验证等
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
RestHighLevelClient获取某个索引的mapping
爬虫代码基础教学
crontab定时任务
Realize PDF to picture conversion with C #
golang操作redis:写入、读取hash类型数据
Laravel frame step pit (I)
EasyExcel
MySQL installation
每日刷題記錄 (十一)
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
Pits encountered in the use of El checkbox group
Unittest attempt