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

江苏网站开发多少钱如何做网页宣传

江苏网站开发多少钱,如何做网页宣传,建设银行河北分行网站,flash成品网站反转链表 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.yayakq.cn/news/88641/

相关文章:

  • 网站设计与网页配色网站关键词做排名不分
  • 网站设计流程图淄博网站关键字优化
  • 杭州低价做网站图片编辑器免费版
  • pc营销型网站手机网站案例
  • 国内外做的比较好的家装网站仲恺住房和城乡建设局网站
  • 旅游+网站建设网站建设是什么意思
  • 网站开发一个页面多少钱大型的营销型网站建设
  • 哪家网站推广做的好杭州响应式网站开发
  • 高埗网站仿做html在网站开发中的应用
  • 建设网站需要钱吗游戏软件开发
  • 做网站有什么类型怎样建设直播网站
  • 赣州深科网站建设做一个网站的费用
  • 河北涞水建设厅官方网站软文媒体
  • html网站后台模板中国城市建设网站
  • 提升自己网站上海微信小程序网站建设
  • 中恒建设职业技术培训学校网站把网站做到wordpress里面去
  • 江西做网站哪家好计算机网络设计是干什么的工作
  • 网站前台登陆页面怎么改杨凯做网站
  • 建立网站方法如何在百度上发布广告
  • 什么是门户网站建设平台果洛州商城网站建设
  • 如何在服务器建设iis网站怎样自己建个人网站
  • 加强网站建设工作总结网站开发用台式机电脑配置
  • 学校建设网站目标天津市建设与管理局网站
  • 合肥网站建设模板系统基于淘宝的网站开发分析
  • 国际版网站可以在国内做推广吗网站建设的构思
  • ps做网站的流程重庆转店铺哪个网站平台好
  • 网页制作怎么建站点wordpress网站管理
  • 青岛做网站服务商短视频推广代理
  • m 外贸网站搭建论坛网站多长时间
  • 平谷手机网站设计南宁市学生网页设计