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

做网络技术方案叫什么优化的概念

做网络技术方案叫什么,优化的概念,国家企业查询系统官网天眼查,买了云服务器怎么做网站【算法系列-链表】设计链表 文章目录 【算法系列-链表】设计链表1. 算法分析🛸2. 解题过程🎬2.1 初始化2.1.1 思路分析🎯2.1.2 代码示例🌰 2.2 get(获取第n个节点的值)2.2.1 思路分析🎯2.2.2 代码示例🌰 2.…

【算法系列-链表】设计链表

文章目录

  • 【算法系列-链表】设计链表
    • 1. 算法分析🛸
    • 2. 解题过程🎬
      • 2.1 初始化
        • 2.1.1 思路分析🎯
        • 2.1.2 代码示例🌰
      • 2.2 get(获取第n个节点的值)
        • 2.2.1 思路分析🎯
        • 2.2.2 代码示例🌰
      • 2.3 addAtHead(头部插入节点)
        • 2.3.1 思路分析🎯
        • 2.3.2 代码示例🌰
      • 2.4 addAtTail(尾部插入节点)
        • 2.4.1 思路分析🎯
        • 2.4.2 代码示例🌰
      • 2.5 addAtIndex(在第n个节点前插入节点)
        • 2.5.1 思路分析🎯
        • 2.5.2 代码示例🌰
      • 2.6 deleteAtIndex(删除第n个节点的节点)
        • 2.6.1 思路分析🎯
        • 2.6.2 代码示例🌰
    • 3. 代码汇总🧮

1. 算法分析🛸

【题目链接】: LeetCode 707 设计链表

这是一道很经典的题,涵盖了链表的常见基本操作,包括:

  • 获取第n个节点的值;
  • 头部插入节点;
  • 尾部插入节点;
  • 在第n个节点前插入节点;
  • 删除第n个节点的节点;

注:下述解题过程中使用到了虚拟头节点的方式进行操作,最开始都是从虚拟头节点开始遍历的,并且在单链表每次寻找节点都只找到目标节点的前一个节点,这样可以方便我们对目标节点进行操作

2. 解题过程🎬

2.1 初始化

2.1.1 思路分析🎯

最开始需要先定义好Node节点类,包括变量:val(节点值) 和 next(当前节点的下一个节点),并设置对应的构造方法方便我们后续创建节点时能够直接赋值; 创建MyLinkedList的构造方法用来初始化 虚拟头节点head链表长度 size

2.1.2 代码示例🌰
class Node {int val;Node next;public Node(){}public Node(int val) {this.val = val;}public Node(int val, Node next) {this.val = val;this.next = next;}
}class MyLinkedList {Node head;int size;public MyLinkedList() {this.head = new Node();size = 0;}...
}

之后编写对应的每个方法:

2.2 get(获取第n个节点的值)

2.2.1 思路分析🎯

当 index 大于 链表长度时,返回-1; 定义count用来表示当前cur所处位置,cur是从虚拟头节点开始遍历的,当count = 0时 cur 正在头节点上; 进入循环,当 cur != null && cur.next != null 时,进行判断: 当 count == index 时,表示当前 cur 的下一个节点就是目标节点 ,返回 cur.next.val 即可 当 count != index 时,cur 继续往下遍历,即 cur = cur.next; 判断结束后 无论结果 count 都要 + 1,重复上述过程直到返回结果

2.2.2 代码示例🌰
public int get(int index) {if (index > size) {return -1;}Node cur = head;int count = 0;while (cur != null && cur.next != null) {if (count++ == index) {return cur.next.val;}cur = cur.next;}return -1;     
}

2.3 addAtHead(头部插入节点)

2.3.1 思路分析🎯

构建 node 节点,并传入参数 val 与 head.next,使得 node节点的下一个节点 指向 当前头节点的下一个节点 之后让头节点的下一个节点指向 node节点,同时链表长度 + 1;

2.3.2 代码示例🌰
public void addAtHead(int val) {Node node = new Node(val, head.next);head.next = node;size++;
}

2.4 addAtTail(尾部插入节点)

2.4.1 思路分析🎯

构建 cur 节点并指向头节点head,后续通过cur进行遍历;构建node节点并赋值val; 进入循环:当 cur.next != null 时,cur继续往下遍历,直到 cur.next == null,表示当前cur已经是链表的最后一个节点了,最后让 cur.next 指向 node节点 即可,同时链表长度 + 1;

2.4.2 代码示例🌰
public void addAtTail(int val) {Node cur = head;Node node = new Node(val);while (cur.next != null) {cur = cur.next;}cur.next = node;size++;
}

2.5 addAtIndex(在第n个节点前插入节点)

2.5.1 思路分析🎯

当 index 大于 链表长度时,返回结果空; 定义count用来表示当前cur所处位置,cur是从虚拟头节点开始遍历的,当count = 0时 cur 正在头节点上; 进入循环,当 cur != null 时,进行判断: 当 count == index 时,表示当前 cur 的下一个位置就是目标插入位 ,构建node节点,并传入参数 val 与 cur.nex t,使得 node节点的下一个节点 指向 当前节点cur的下一个节点,之后让 cur的下一个节点指向当前 node节点,以此建立连接,最后 链表长度 + 1,返回结果空; 当 count != index 时,cur 继续往下遍历,即 cur = cur.next;

2.5.2 代码示例🌰
public void addAtIndex(int index, int val) {if (index > size) {return;}int count = 0;Node cur = head;while (cur != null) {if (count++ == index) {Node node = new Node(val, cur.next);cur.next = node;size++;return;}cur = cur.next;}
}

2.6 deleteAtIndex(删除第n个节点的节点)

2.6.1 思路分析🎯

当 index 大于 链表长度时,返回结果空; 定义count用来表示当前cur所处位置,cur是从虚拟头节点开始遍历的,当count = 0时 cur 正在头节点上; 进入循环,当 cur != null && cur.next != null 时,进行判断: 当 count == index 时,表示当前 cur 的下一个节点就是目标删除节点,则让 cur 的下一个节点指向 cur 的下一个节点的下一个节点,以此删除节点连接,最后链表长度 - 1,返回结果空; 当 count != index 时,cur 继续往下遍历,即 cur = cur.next;

2.6.2 代码示例🌰
public void deleteAtIndex(int index) {if (index > size) {return;}Node cur = head;int count = 0;while (cur != null && cur.next != null) {if (count++ == index) {cur.next = cur.next.next;size--;return;}cur = cur.next;}
}

3. 代码汇总🧮

class Node {int val;Node next;public Node(){}public Node(int val) {this.val = val;}public Node(int val, Node next) {this.val = val;this.next = next;}
}class MyLinkedList {Node head;int size;public MyLinkedList() {this.head = new Node();size = 0;}public int get(int index) {if (index > size) {return -1;}Node cur = head;int count = 0;while (cur != null && cur.next != null) {if (count++ == index) {return cur.next.val;}cur = cur.next;}return -1;}public void addAtHead(int val) {Node node = new Node(val, head.next);head.next = node;size++;}public void addAtTail(int val) {Node cur = head;Node node = new Node(val);while (cur.next != null) {cur = cur.next;}cur.next = node;size++;}public void addAtIndex(int index, int val) {if (index > size) {return;}int count = 0;Node cur = head;while (cur != null) {if (count++ == index) {Node node = new Node(val, cur.next);cur.next = node;size++;return;}cur = cur.next;}}public void deleteAtIndex(int index) {if (index > size) {return;}Node cur = head;int count = 0;while (cur != null && cur.next != null) {if (count++ == index) {cur.next = cur.next.next;size--;return;}cur = cur.next;}}
}

以上便是对设计链表类型题的介绍了!!后续还会继续分享其它算法系列内容,如果这些内容对大家有帮助的话请给一个三连关注吧💕( •̀ ω •́ )✧( •̀ ω •́ )✧✨

http://www.yayakq.cn/news/613962/

相关文章:

  • 建设企业网站个人网银wordpress需要备案号
  • 邮轮哪个网站是可以做特价免费做爰网站
  • steam账号注册网站网站开发有哪些服务器
  • 伍佰亿网站怎么样建立个人免费网站
  • ssh购物网站开发视频商城网站开发模板
  • 用c 做网站城乡建设和住房建设部八大员
  • 网站建设 锐颖科技网络公司是做什么的?
  • asp网站用什么做第一成品网站
  • 品牌网站建设只詢大蝌蚪wap网站建设方案
  • 门户网站建设情况汇报工具磨床东莞网站建设
  • 网站转化率偏低怎么办西安大型网站建设公司
  • 推广网站推广广州建设网站公司简介
  • 做视频网站的方法抚州城乡建设厅网站
  • 手机产品展示网站模板wordpress后台作用
  • 网站建设模块dedecms 网站地图模板
  • 中化建工北京建设投资有限公司网站什么是网络营销的新特点
  • 展览公司网站建设方案企业建设网站需注意哪些内容
  • 网站备案 新增如何创建一个企业网站
  • 公司网站开发策划域名网站大全
  • 外贸网站建设如何做呢简约wordpress主题
  • 简述企业网站建设的目的有哪些选择好的软件开发培训班
  • 营销型网站建设价格私人定制
  • wordpress多语言网站php做的网站毕设会问的问题
  • 腾宁网络做网站wordpress html压缩
  • 平顶山 网站建设公司网站ui升级怎么做
  • aspx网站服务器失去响应番禺网站建设公司排名
  • 湖南网站设计外包哪家好搜索网站模板
  • 我想做个网站吉林省吉林市
  • 网站运营做网站需要多长时间
  • 有做装修效果图赚钱的网站吗宁波网站建设 泊浮科技