当前位置:网站首页>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;
}
}
边栏推荐
- [set theory] partition (partition | partition example | partition and equivalence relationship)
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
- 卡特兰数(Catalan)的应用场景
- C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
- VMware virtual machine C disk expansion
- Selenium - by changing the window size, the width, height and length of different models will be different
- POI excel percentage
- Ruoyi interface permission verification
- Basic teaching of crawler code
- [LeetCode]404. Sum of left leaves
猜你喜欢
![[classes and objects] explain classes and objects in simple terms](/img/41/250457530880dfe3728432c2ccd50b.png)
[classes and objects] explain classes and objects in simple terms

EasyExcel

How to migrate or replicate VMware virtual machine systems

Mise en place d'un environnement de développement de fonctions personnalisées

Summary of remote connection of MySQL

Inno setup production and installation package

Yolov3 learning notes

2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)

Realize PDF to picture conversion with C #

Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
随机推荐
Asynchronous programming: async/await in asp Net
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
golang操作redis:写入、读取kv数据
利用C#实现Pdf转图片
Laravel Web框架
MySQL transaction rollback, error points record
JMeter JSON extractor extracts two parameters at the same time
CentOS php7.3 installing redis extensions
New knowledge! The virtual machine network card causes your DNS resolution to slow down
Liang Ning: 30 lectures on brain map notes for growth thinking
Software testing learning - day 3
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
DBNet:具有可微分二值化的实时场景文本检测
2022 cisp-pte (III) command execution
php安装swoole扩展
Yolov2 learning and summary
crontab定时任务
File links cannot be opened or downloaded in Google browser
IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library