当前位置:网站首页>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
边栏推荐
- [simple understanding of database]
- Double pointer advanced -- leetcode title -- container with the most water
- Entity Framework core captures database changes
- C语言函数详解
- Self use learning notes - connected and non connected access to database
- Some problems encountered in recent programming 2021 / 9 / 8
- 开期货,开户云安全还是相信期货公司的软件?
- Promise (I)
- Promise (III)
- Open futures, open an account, cloud security or trust the software of futures companies?
猜你喜欢
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Devexpress GridView add select all columns
线性代数感悟之1
HCIP第五次实验
索引:手把手教你索引从零基础到精通使用
PC电脑使用无线网卡连接上手机热点,为什么不能上网
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
PC uses wireless network card to connect to mobile phone hotspot. Why can't you surf the Internet
Tdan over half
Future 用法详解
随机推荐
Using quartz under. Net core - [1] quick start
超分之TDAN
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
EF core in ASP Generate core priority database based on net entity model
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
01-初识sketch-sketch优势
[simple understanding of database]
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
Self use learning notes - connectingstring configuration
JS to find the character that appears three times in the string
Come out after a thousand calls
stm32入门开发板选野火还是正点原子呢?
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
Seven cattle upload pictures (foreground JS + background C API get token)
圆环回原点问题-字节跳动高频题
Signalr can actively send data from the server to the client
開期貨,開戶雲安全還是相信期貨公司的軟件?
古代埃及希腊,数学用的什么进制
How to use the input table one-way service to send (occupy less) picture files (body transmission)? FileReader built-in object involved
Construction of functions in C language programming