当前位置:网站首页>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;
}
}
边栏推荐
- Centos切换安装mysql5.7和mysql8.0
- Asynchronous programming: async/await in asp Net
- (翻译)异步编程:Async/Await在ASP.NET中的介绍
- IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
- Yolov1 learning notes
- On the practice of performance optimization and stability guarantee
- centos php7.2.24升级到php7.3
- Realize PDF to picture conversion with C #
- Laravel frame step pit (I)
- Sorting out the core ideas of the pyramid principle
猜你喜欢

10000小時定律不會讓你成為編程大師,但至少是個好的起點

2022年华东师范大学计科考研复试机试题-详细题解

In depth analysis of reentrantlock fair lock and unfair lock source code implementation

Inno Setup 制作安装包

Create your own deep learning environment with CONDA

Software testing learning - the next day

Dbnet: real time scene text detection with differentiable binarization

Summary of the design and implementation of the weapon system similar to the paladin of vitality

HMS core helps baby bus show high-quality children's digital content to global developers

卡特兰数(Catalan)的应用场景
随机推荐
Jenkins
How does the insurance company check hypertension?
Operation principle of lua on C: Foundation
[untitled]
每日刷題記錄 (十一)
Create your own deep learning environment with CONDA
[LeetCode]404. 左叶子之和
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Inno setup production and installation package
How to migrate or replicate VMware virtual machine systems
Arctic code vault contributor
Personally design a highly concurrent seckill system
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Understand software testing
Summary of UI module design and practical application of agent mode
[Fiddler actual operation] how to use Fiddler to capture packets on Apple Mobile Phones
php安装swoole扩展
php安装composer
MySQL mistakenly deleted the root account and failed to log in