当前位置: 首页 > news >正文

算法leetcode|92. 反转链表 II(rust重拳出击)


文章目录

  • 92. 反转链表 II:
    • 样例 1:
    • 样例 2:
    • 提示:
    • 进阶:
  • 分析:
  • 题解:
    • rust:
    • go:
    • c++:
    • python:
    • java:


92. 反转链表 II:

给你单链表的头指针 head 和两个整数 leftright ,其中 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]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶:

  • 你可以使用一趟扫描完成反转吗?
  • 将链表分成3部分,即前面不需要反转的部分,中间需要反转的部分,后面不需要反转的部分。
  • 单向链表只能从一个方向遍历,并且不能随机访问,所以我们只能按照顺序处理,这里就不能有什么好的技巧了。
  • 先处理前面不需要反转的部分,直接遍历跳过即可,这里由于没法随机访问,所以只能按顺序遍历。
  • 接下来处理中间需要反转的部分,想象一下入栈,遍历一个节点就放到头(中间需要遍历部分的头),依次遍历处理,就完成了反转。
  • 最后处理后面不需要遍历的部分,其实这部分不需要做什么处理,所以不需要继续遍历,但是要当心将链表接起来。
  • 这里不得不说由于rust的所有权问题,同一时间只能有一个可修改指针,可能是二当家水平太菜,处理链表有点啰嗦。

分析:

  • 面对这道算法题目,二当家的再次陷入了沉思。

题解:

rust:

// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {pub fn reverse_between(head: Option<Box<ListNode>>, left: i32, right: i32) -> Option<Box<ListNode>> {// 来个哑节点方便处理头节点在反转范围内的情况let mut dummy = Option::Some(Box::new(ListNode::new(0)));dummy.as_mut().unwrap().next = head;// 前面不需要反转部分的尾let mut pre = dummy.as_mut().unwrap();// 跳过前面不需要翻转的部分for _ in 0..left - 1 {pre = pre.next.as_mut().unwrap();}// 中间需要反转的部分(同时打断前面不需要反转部分和中间需要反转部分的链接)let mut cur = pre.next.take();// 进行反转for _ in 0..right - left {let mut next = cur.as_mut().unwrap().next.take();cur.as_mut().unwrap().next = next.as_mut().unwrap().next.take();next.as_mut().unwrap().next = pre.next.take();pre.next = next;}// 重新接上while pre.next.is_some() {pre = pre.next.as_mut().unwrap();}pre.next = cur;return dummy.unwrap().next;}
}

go:

/*** Definition for singly-linked list.* type ListNode struct {*     Val int*     Next *ListNode* }*/
func reverseBetween(head *ListNode, left int, right int) *ListNode {// 来个哑节点方便处理头节点在反转范围内的情况dummy := &ListNode{Val: 0, Next: head}pre := dummyfor i := 0; i < left-1; i++ {pre = pre.Next}cur := pre.Nextfor i := 0; i < right-left; i++ {next := cur.Nextcur.Next = next.Nextnext.Next = pre.Nextpre.Next = next}return dummy.Next
}

c++:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* reverseBetween(ListNode* head, int left, int right) {// 来个哑节点方便处理头节点在反转范围内的情况ListNode *dummy = new ListNode(0);dummy->next = head;ListNode *pre = dummy;for (int i = 0; i < left - 1; ++i) {pre = pre->next;}ListNode *cur = pre->next;ListNode *next;for (int i = 0; i < right - left; ++i) {next = cur->next;cur->next = next->next;next->next = pre->next;pre->next = next;}return dummy->next;}
};

python:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:# 来个哑节点方便处理头节点在反转范围内的情况dummy = ListNode(0)dummy.next = headpre = dummyfor _ in range(left - 1):pre = pre.nextcur = pre.nextfor _ in range(right - left):next = cur.nextcur.next = next.nextnext.next = pre.nextpre.next = nextreturn dummy.next

java:

/*** 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 dummy = new ListNode(0);dummy.next = head;ListNode pre = dummy;for (int i = 0; i < left - 1; i++) {pre = pre.next;}ListNode cur = pre.next;ListNode next;for (int i = 0; i < right - left; i++) {next = cur.next;cur.next = next.next;next.next = pre.next;pre.next = next;}return dummy.next;}
}

非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】三连走一波~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~


http://www.lryc.cn/news/261106.html

相关文章:

  • Chapter 7 - 3. Congestion Management in Ethernet Storage Networks以太网存储网络的拥塞管理
  • 优雅玩转实验室服务器(二)传输文件
  • 动态面板简介以及ERP原型图案列
  • 漏刻有时百度地图API实战开发(12)(切片工具的使用、添加自定义图层TileLayer)
  • python 爬虫 m3u8 视频文件 加密解密 整合mp4
  • mybatis中xml文件容易搞混的属性
  • android Retrofit2.0请求 延长超时操作
  • Axure之动态面板轮播图
  • 一文读懂算法中的时间复杂度和空间复杂度,O(1)、O(logn)、O(n)、O(n^2)、O(2^n) 附举例说明,常见的时间复杂度,空间复杂度
  • LWIP热插拔功能实现
  • android下的app性能测试应主要针对那些方面,如何开展?
  • 【深度学习】注意力机制(二)
  • 学习黑马vue
  • gdb本地调试版本移植至ARM-Linux系统
  • 《Linux C编程实战》笔记:实现自己的ls命令
  • Python个人代码随笔(观看无益,请跳过)
  • Unity中实现ShaderToy卡通火(总结篇)
  • 等保2.0的变化
  • 漏洞复现-网神SecGate3600防火墙敏感信息泄露漏洞(附漏洞检测脚本)
  • ArkTS入门
  • JS中for循环之退出循环
  • 《Global illumination with radiance regression functions》
  • 华南理工C++试卷
  • 0001.WIN7(64位)安装ADS1.2出现L6218错误
  • HBuilderX 配置 夜神模拟器 详细图文教程
  • 10、神秘的“位移主题”
  • 【Linux】dump命令使用
  • 使用 TensorFlow 创建生产级机器学习模型(基于数据流编程的符号数学系统)——学习笔记
  • vue实现悬浮窗拖动的自定义指令
  • gitee(ssh)同步本地