当前位置:网站首页>92. 反转链表 II-字节跳动高频题
92. 反转链表 II-字节跳动高频题
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]
示例 2:
输入:head = [5], left = 1, right = 1
输出:[5]
二、解题
链表拆分+合并

先找到上面四个节点值,然后preleft和leftnode断开,然后在将rightnode和nextright断开,然后将leftnode和rightnode中的链表翻转,然后在合并链表。
/** * 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) {
//定义两个节点,left 的前一个节点,right的后一个节点
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode prelfet = dummy;
//查找四个节点
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;
//断开链表
prelfet.next = null;
rightnode.next = null;
//翻转链表
ListNode cur = reverse(leftnode);
//合并链表
prelfet.next = cur;
leftnode.next = nextright;
return dummy.next;
}
//翻转链表有两种 递归和迭代
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://blog.csdn.net/hequnwang10/article/details/124220472
边栏推荐
- stm32入门开发板选野火还是正点原子呢?
- ClickHouse-数据类型
- [batch change MySQL table and corresponding codes of fields in the table]
- Manually implement simple promise and its basic functions
- C dapper basically uses addition, deletion, modification and query transactions, etc
- ASP. Net core JWT certification
- Understanding and small examples of unity3d object pool
- 双闭环直流调速系统matlab/simulink仿真
- Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
- 索引:手把手教你索引从零基础到精通使用
猜你喜欢
![[WPF binding 3] listview basic binding and data template binding](/img/2e/fbdb4175297bb4964a8ccfd0b909ae.png)
[WPF binding 3] listview basic binding and data template binding

超分之TDAN

Use of five routing guards

Advantages and disadvantages of several note taking software
![[ES6] promise related (event loop, macro / micro task, promise, await / await)](/img/69/ea3ef6063d373f116a44c53565daa3.png)
[ES6] promise related (event loop, macro / micro task, promise, await / await)

. net cross platform principle (Part I)

Simulation of infrared wireless communication based on 51 single chip microcomputer

2. Electron's HelloWorld

常用SQL语句总结

线性代数感悟之1
随机推荐
读《Software Engineering at Google》(15)
Double pointer advanced -- leetcode title -- container with the most water
C语言程序设计之函数的构造
Shell-入门、变量、以及基本的语法
Further optimize Baidu map data visualization
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
为什么有些人说单片机简单,我学起来这么吃力?
Excel quickly and automatically fills the contents of a row on a blank cell
Entity Framework core captures database changes
常用SQL语句总结
Wiper component encapsulation
Compare the performance of query based on the number of paging data that meet the query conditions
uni-app黑马优购项目学习记录(下)
Matlab / Simulink simulation of double closed loop DC speed regulation system
If you start from zero according to the frame
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Router object, route object, declarative navigation, programmed navigation
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
Use of five routing guards
Generating access keys using JSON webtoken