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

广州增城区最新消息windows优化大师好不好

广州增城区最新消息,windows优化大师好不好,沭阳网站建设,html5手机企业网站模板反转链表 II https://leetcode.cn/problems/reverse-linked-list-ii/description/ 描述 给你单链表的头指针 head 和两个整数 left 和 right &#xff0c;其中 left < right请你反转从位置 left 到位置 right 的链表节点&#xff0c;返回 反转后的链表 示例 1 输入&…

反转链表 II

  • https://leetcode.cn/problems/reverse-linked-list-ii/description/

描述

  • 给你单链表的头指针 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]

提示

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n
  • 进阶: 你可以使用一趟扫描完成反转吗?

Typescript 版算法实现


1 ) 方案1: 穿针引线

/*** Definition for singly-linked list.* class ListNode {*     val: number*     next: ListNode | null*     constructor(val?: number, next?: ListNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.next = (next===undefined ? null : next)*     }* }*/const reverseLinkedList = (head: ListNode | null) => {let pre = null;let cur = head;while (cur) {const next = cur.next;cur.next = pre;pre = cur;cur = next;}
}function reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null {// 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论const dummyNode = new ListNode(-1);dummyNode.next = head;let pre = dummyNode;// 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点// 建议写在 for 循环里,语义清晰for (let i = 0; i < left - 1; i++) {pre = pre.next;}// 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点let rightNode = pre;for (let i = 0; i < right - left + 1; i++) {rightNode = rightNode.next;}// 第 3 步:切断出一个子链表(截取链表)let leftNode = pre.next;let curr = rightNode.next;// 注意:切断链接pre.next = null;rightNode.next = null;// 第 4 步:同第 206 题,反转链表的子区间reverseLinkedList(leftNode);// 第 5 步:接回到原来的链表中pre.next = rightNode;leftNode.next = curr;return dummyNode.next;
};

2 ) 方案2: 一次遍历「穿针引线」反转链表(头插法)

/*** Definition for singly-linked list.* class ListNode {*     val: number*     next: ListNode | null*     constructor(val?: number, next?: ListNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.next = (next===undefined ? null : next)*     }* }*/function reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null {// 设置 dummyNode 是这一类问题的一般做法const dummy_node = new ListNode(-1);dummy_node.next = head;let pre = dummy_node;for (let i = 0; i < left - 1; ++i) {pre = pre.next;}let cur = pre.next;for (let i = 0; i < right - left; ++i) {const next = cur.next;cur.next = next.next;next.next = pre.next;pre.next = next;}return dummy_node.next;
};

3 )方案3:局部反转法

/*** Definition for singly-linked list.* class ListNode {*     val: number*     next: ListNode | null*     constructor(val?: number, next?: ListNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.next = (next===undefined ? null : next)*     }* }*/function reverseBetween(head: ListNode | null, left: number, right: number): ListNode | null {const dummy = {next: head}let tmp = dummyfor (let i = 0; i < left - 1; i++) {tmp = tmp.next}let prev = tmp.nextlet cur = prev.nextfor (let j = 0; j < right - left; j++) {let next = cur.nextcur.next = prevprev = curcur = next // cur = cur.next}tmp.next.next = curtmp.next = prevreturn dummy.next
};
http://www.hkea.cn/news/89161/

相关文章:

  • 济南网站建设那家好网站制作公司有哪些
  • 域名和网站名不一样营销公司
  • discuz做电影网站免费网站seo
  • 惠民建设局网站明年2024年有疫情吗
  • 卫龙的网站是谁做的今日的新闻
  • 厚街找人做网站动态网站设计
  • 永春县住房和城乡规划建设局网站太原seo排名优化软件
  • 怎么上网站后台爱站小工具计算器
  • 网页编辑岗位职责seo上海优化
  • 网站做二维码吗做网站的外包公司
  • 郑州市中原区疫情最新消息上海网站营销seo方案
  • 狂人站群系统中国最权威的网站排名
  • 简单网站开发实例网站运营工作的基本内容
  • 飞机免费代理ip爱站网seo综合查询工具
  • 河南焦作有做网站开发的公司吗巩义网络推广公司
  • 邓州做网站网络广告有哪些形式
  • 爬闪数媒 网站建设网站建站流程
  • 网站建设广州白云百度统计app下载
  • 惠州短视频seoseowhy论坛
  • 肇庆网站快速排名优化温州seo排名公司
  • 北京疫情死亡人数最新消息王通seo赚钱培训
  • 北京做网站的外包公司营销策划方案案例范文
  • 专业做酒店网站关键词优化排名软件流量词
  • 做网站推广代理上海网络推广服务
  • wordpress可以做大吗搜索引擎优化的英语简称
  • 民治专业做网站公司中国企业500强排行榜
  • 潍坊 公司 网站seo点击排名器
  • 网站可以做赌博广告建站宝盒
  • 运城市做网站英文seo外链
  • 江宁网站建设如何建立网上销售平台