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

JS 链表

文章目录

  • 链表题的一些总结
    • 两种链表定义
    • set存储链表节点,存的是整个空间
    • 同时处理长短不一的两个链表
      • 处理方法 while(l1 || l2)
      • 处理方法 while(l1 & l2)
    • dummyhead的使用

链表题的一些总结

两种链表定义

  • class
class ListNode {val;next = null;constructor(value) {this.val = value;this.next = null;}
}
  • function
function ListNode(val, next) {this.val = val === undefined ? 0 : val;this.next = next === undefined ? null : next;
}

set存储链表节点,存的是整个空间

  • 相交链表
/*** Definition for singly-linked list.* function ListNode(val) {*     this.val = val;*     this.next = null;* }*//*** @param {ListNode} headA* @param {ListNode} headB* @return {ListNode}*/
var getIntersectionNode = function(headA, headB) {const set = new Set();let tmp = headA;while(tmp) {if(!set.has(tmp)) {set.add(tmp);}tmp = tmp.next;}tmp = headB;while(tmp) {if(set.has(tmp)) {return tmp;}tmp = tmp.next;  }return null;
};

同时处理长短不一的两个链表

处理方法 while(l1 || l2)

  • 两数相加
/*** Definition for singly-linked list.* function ListNode(val, next) {*     this.val = (val===undefined ? 0 : val)*     this.next = (next===undefined ? null : next)* }*/
/*** @param {ListNode} l1* @param {ListNode} l2* @return {ListNode}*/
var addTwoNumbers = function(l1, l2) {let head = new ListNode(0);let cur = head;let carry = 0;while(l1 != null || l2 != null) {const n1 = l1 != null ? l1.val : 0;const n2 = l2 != null ? l2.val : 0;let sum = n1 + n2 + carry;carry = Math.floor(sum / 10);sum = sum % 10;let tmp = new ListNode(sum, null);cur.next = tmp;cur = tmp;if(l1 != null) {l1 = l1.next;}if(l2 != null) {l2 = l2.next;}}if(carry === 1) {let tmp = new ListNode(1, null);cur.next = tmp;cur = tmp;}return head.next;
};

处理方法 while(l1 & l2)

不能用或,如果一个链表之后为空了,就没有比较的必要

  • 合并有序链表
  • 超出时间限制
var mergeTwoLists = function(list1, list2) {let dummyhead = new ListNode(-1);let h = dummyhead;while(list1 || list2) {if(list1 === null) {h.next = list2;} else if(list2 === null) {h.next = list1;} else {if(list1.val < list2.val) {h.next = list1;h = list1;list1 = list1.next;} else {h.next = list2;h = list2;list2 = list2.next;}}}return h.next;
};
  • 不超出时间限制
/*** Definition for singly-linked list.* function ListNode(val, next) {*     this.val = (val===undefined ? 0 : val)*     this.next = (next===undefined ? null : next)* }*/
/*** @param {ListNode} list1* @param {ListNode} list2* @return {ListNode}*/
var mergeTwoLists = function(list1, list2) {let dummyhead = new ListNode(-1);let h = dummyhead;while(list1 && list2) {if(list1.val < list2.val) {h.next = list1;h = list1;list1 = list1.next;} else {h.next = list2;h = list2;list2 = list2.next;}}if(list1 === null) {h.next = list2;} else if(list2 === null) {h.next = list1;}return dummyhead.next;
};

dummyhead的使用

返回时 dummyhead.next

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

相关文章:

  • 数据结构(陈越,何钦铭)第三讲 树(上)
  • 企业文件安全:零信任架构下的文件访问控制
  • 性格测评小程序06用户注册校验
  • $符(前端)
  • Windows 11如何显示全部右键菜单?
  • 离线量化算法和工具 --学习记录1
  • python第七课
  • 华为IPD简介
  • 如何在Spring Boot中配置分布式配置中心
  • Golang internals
  • 天翼云910B部署DeepSeek蒸馏70B LLaMA模型实践总结
  • 数据治理常用的开源项目有哪些?
  • 数据结构与算法之排序算法-(计数,桶,基数排序)
  • Word正文中每两个字符之间插入一个英文半角空格
  • 把 DeepSeek1.5b 部署在显卡小于4G的电脑上
  • A4988一款带转换器和过流保护的 DMOS 微步驱动器的使用方式
  • 一口井深7米,一只蜗牛从井底往上爬每天爬3米掉下去1米,问几天能爬上井口?
  • Asp.Net Core MVC 中级开发教程
  • Windows上安装Go并配置环境变量(图文步骤)
  • C++效率掌握之STL库:string底层剖析
  • 【Erdas实验教程】004:影像镶嵌拼接
  • SpringMVC 请求参数接收
  • [高等数学]换元积分法
  • Redis简介、常用命令及优化
  • 大模型训练为什么依赖GPU
  • 帕金森病与三叉神经痛的基因关联分析
  • 【Android开发】华为手机安装包安装失败“应用是非正式版发布版本,当前设备不支持安装”问题解决
  • 栈与队列(C语言版)
  • stl里的deque 中控map 假如用完了,该如何处理
  • Git GUI设置中文的方法及使用