当前位置:网站首页>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
边栏推荐
- ASP. NET CORE3. 1. Solution to login failure after identity registers users
- Qt 修改UI没有生效
- In embedded system, must the program code in flash be moved to ram to run?
- matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
- 1217_使用SCons生成目标文件
- uni-app黑马优购项目学习记录(下)
- Promise (I)
- 1-4 configuration executable script of nodejs installation
- 1-1 NodeJS
- 读《Software Engineering at Google》(15)
猜你喜欢

Exercise: even sum, threshold segmentation and difference (two basic questions of list object)

.Net Core3. 1 use razorengine NETCORE production entity generator (MVC web version)

Why do some people say SCM is simple and I have to learn it so hard?

Summary of common SQL statements

Simulation of infrared wireless communication based on 51 single chip microcomputer

C语言函数详解

Use of todesk remote control software

STM32 entry development board choose wildfire or punctual atom?

HCIP第五次实验

索引:手把手教你索引从零基础到精通使用
随机推荐
Use of todesk remote control software
Solution of Navicat connecting Oracle library is not loaded
Use of shell sed command
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
Self use learning notes - connected and non connected access to database
Webapi + form form upload file
[ES6] promise related (event loop, macro / micro task, promise, await / await)
1-3 nodejs installation list configuration and project environment
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
Detailed explanation of C webpai route
Websocket (basic)
Basic case of Baidu map
Learning record of uni app dark horse yougou project (Part 2)
Excel quickly and automatically fills the contents of a row on a blank cell
How to sort the numbers with text in Excel from small to large instead of the first number
402. 移掉 K 位数字-贪心
开期货,开户云安全还是相信期货公司的软件?
Node template engine (EJS, art template)
Promise (IV)
线性代数感悟之2