当前位置:网站首页>leetcode 92. Reverse linked list II
leetcode 92. Reverse linked list II
2022-06-27 16:59:00 【chenyson】
difficulty : secondary
The frequency of :116
subject : Give you the head pointer of the single linked list head And two integers left and right , among left <= right . Please reverse from position left To the position right The linked list node of , return Inverted list .
Their thinking : Reverse a linked list + Traverse
Be careful :
- Simple implementation of linked list inversion 【 The first interpolation , There are many other ways , But the error rate is low , There are many places to use 】
- Virtual header node However, it is necessary to consider the left and right boundary nodes , It's right to use virtual nodes
- In fact, that is g p Two fixed nodes , Then a mobile node temp
- g Is used to connect inverted nodes ,p The node of has only one function , Point to the next node that needs to be reversed
- This head insertion method , The last part is also connected
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode reverseList(ListNode head) {
ListNode dummyhead=new ListNode(-1);
dummyhead.next=head;
ListNode p=head,g=dummyhead;
if(head==null) return head;
while(p.next!=null){
// Here is chosen p.next A big reason is p Point to the next node to be reversed
ListNode temp=p.next;
p.next=temp.next;
temp.next=g.next;
g.next=temp;
}
return dummyhead.next;
}
}
- The number of numbers between two numbers : x To y There are many numbers in total =y-x+1, such as 1-100 Altogether =100-1+1 A digital
- Move first g and p To where we need to reverse
Code
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummyhead=new ListNode(-1);
dummyhead.next=head;
ListNode g=dummyhead,p=head;
for(int i=0;i<left-1;i++){
//p spot ( Actually, the location is 1) Number of moves from left-1+1-1
g=g.next;
p=p.next;
}
for(int j=0;j<right-left+1-1;j++){
ListNode temp=p.next;
p.next=temp.next;
temp.next=g.next;
g.next=temp;
//p.g It's all immovable , Only other nodes move
}
return dummyhead.next;
}
}
边栏推荐
- #yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
- LeetCode每日一练(两数之和)
- LeetCode 124. Binary tree maximum path sum - binary tree series question 8
- 关于#mysql#的问题:问题遇到的现象和发生背景
- 06. First introduction to express
- [the way of programmer training] - 3 Character count statistics
- Relation and operation of ORM table
- C language set operation
- Byte + Google super full kotlin learning King fried notes! Kotlin introductory tutorial + Advanced kotlin enhanced actual combat (with demo)
- Leetcode 704. Binary search
猜你喜欢

Julia constructs diagonal matrix

LeetCode每日一练(两数之和)

【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。

数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】

模拟进程调度

Simulated process scheduling
P. Simple application of a.r.a method in Siyuan (friendly testing)

About MySQL: the phenomenon and background of the problem

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

防火墙基础之源NAT地址转换和服务器映射web页面配置
随机推荐
Determine the maximum number of specific words in a string
Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
P.A.R.A 方法在思源的简易应用(亲测好用)
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
树莓派初步使用
EMQ 助力青岛研博建设智慧水务平台
The European unified charging specification act was passed before the end of the year, and it is planned to expand to products such as laptop and keyboard
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
C language course design
鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
Cesium realizes satellite orbit detour
d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?
Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
Delete duplicate elements in the sorting linked list
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
C système de gestion de la charge de travail des enseignants en langues