当前位置:网站首页>92. Reverse linked list II byte skipping high frequency question
92. Reverse linked list II byte skipping high frequency question
2022-04-23 17:32:00 【hequnwang10】
One 、 Title Description
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 .
Example 1:
Input :head = [1,2,3,4,5], left = 2, right = 4
Output :[1,4,3,2,5]
Example 2:
Input :head = [5], left = 1, right = 1
Output :[5]
Two 、 Problem solving
Linked list splitting + Merge
First find the above four node values , then preleft and leftnode To break off , And then the rightnode and nextright To break off , And then leftnode and rightnode Flip the linked list in , Then merge the linked list .
/** * 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) {
// Define two nodes ,left Previous node of ,right Next node of
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prelfet = dummy;
// Find four nodes
for(int i = 0;i<left-1;i++){
prelfet = prelfet.next;
}
ListNode leftnode = prelfet.next;
ListNode rightnode = prelfet;
for(int i = 0;i<right-left+1;i++){
rightnode = rightnode.next;
}
ListNode nextright = rightnode.next;
// Disconnect list
prelfet.next = null;
rightnode.next = null;
// Flip list
ListNode cur = reverse(leftnode);
// Merge list
prelfet.next = cur;
leftnode.next = nextright;
return dummy.next;
}
// There are two ways to flip a linked list Recursion and iteration
public ListNode reverse(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode cur = reverse(head.next);
head.next.next = head;
head.next = null;
return cur;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009810.html
边栏推荐
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- EF core in ASP Generate core priority database based on net entity model
- 239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
- Clickhouse - data type
- ECMAScript history
- In ancient Egypt and Greece, what base system was used in mathematics
- [WPF binding 3] listview basic binding and data template binding
- Conversion between hexadecimal numbers
- Wiper component encapsulation
- QT modification UI does not take effect
猜你喜欢
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
JVM类加载机制
STM32 entry development board choose wildfire or punctual atom?
Scope and scope chain in JS
Devexpress GridView add select all columns
Bottom processing of stack memory in browser
Signalr can actively send data from the server to the client
Further study of data visualization
ASP. Net core JWT certification
随机推荐
Wiper component encapsulation
If you start from zero according to the frame
Understanding of RPC core concepts
Using quartz under. Net core - [1] quick start
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
C# Task. Delay and thread The difference between sleep
[difference between Oracle and MySQL]
Node template engine (EJS, art template)
ASP. Net core configuration options (Part 1)
Open futures, open an account, cloud security or trust the software of futures companies?
Detailed explanation of C webpai route
Tdan over half
Baidu Map Case - modify map style
Shell - introduction, variables, and basic syntax
PC电脑使用无线网卡连接上手机热点,为什么不能上网
Change Oracle to MySQL
Clickhouse SQL operation
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
Scope and scope chain in JS