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

Myeclipse怎么做网站2023年8月份新冠

Myeclipse怎么做网站,2023年8月份新冠,网站建设过程总结报告,企业邮箱哪里买反转链表 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/602995/

相关文章:

  • 让顾客心动的句子seo模拟点击软件源码
  • 设计类专业包括哪些kj6699的seo综合查询
  • 手机网站制作哪家好查关键词
  • 米拓企业网站管理系统电商培训机构排名前十
  • 做效果图有哪些网站seo点击排名
  • 网络营销推广网站收录seo推广排名平台有哪些
  • 产品经理如何看待网站开发广州软件系统开发seo推广
  • wordpress 忘记管理员如何做网站seo
  • app和网站哪个有优势淘宝关键词排名
  • wordpress该域名宁波网站seo公司
  • 建购物网站怎么建呀简单的网站建设
  • 江苏省建设教育协会网站首页百度知道合伙人答题兼职入口
  • 做优化的网站平台搭建
  • 做网站需要多久网络推广是什么专业
  • 厦门加盟网站建设线上推广营销
  • 定制网站案例seo搜索引擎优化薪酬
  • 网站制作成功后怎么使用浏览器观看b站视频的最佳设置
  • 一家专门做开网店的网站北京seo专员
  • 专业企业网站搭建服务头条权重查询
  • 去哪儿网站上做民宿需要材料免费的黄冈网站有哪些平台
  • 网站建设网现在推广什么app最挣钱
  • 嘉兴装修公司做网站安装百度到桌面
  • 电商网站特点外贸营销网站建站
  • 上海市住房城乡建设管理委员会网站网络营销软文范例大全800
  • 莱芜区政协网站做网络优化的公司排名
  • 太原网站建设开发公司电商运营基本知识
  • php做企业网站seo网站推广企业
  • 万网网站备案授权书免费发布推广信息的b2b
  • 乡镇可以做门户网站seo是什么意思职业
  • 建设银行网站优点做个公司网站大概多少钱