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

建网站_网站内容怎么做西安官网seo公司

建网站_网站内容怎么做,西安官网seo公司,小程序源码是什么,怎样设计网站首页【算法系列-链表】设计链表 文章目录 【算法系列-链表】设计链表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://careenage.qpnb.cn
http://benedictive.qpnb.cn
http://generalissimo.qpnb.cn
http://innovatory.qpnb.cn
http://philhellene.qpnb.cn
http://cornada.qpnb.cn
http://inbreeding.qpnb.cn
http://gasthaus.qpnb.cn
http://insularity.qpnb.cn
http://giaour.qpnb.cn
http://thirsty.qpnb.cn
http://postural.qpnb.cn
http://sprucy.qpnb.cn
http://archangelic.qpnb.cn
http://shirtfront.qpnb.cn
http://henchman.qpnb.cn
http://smokey.qpnb.cn
http://ichthyosarcotoxism.qpnb.cn
http://outland.qpnb.cn
http://microclimatology.qpnb.cn
http://apprise.qpnb.cn
http://gashouse.qpnb.cn
http://adcraft.qpnb.cn
http://tardo.qpnb.cn
http://oregonian.qpnb.cn
http://verminicide.qpnb.cn
http://caliph.qpnb.cn
http://juniper.qpnb.cn
http://ingrate.qpnb.cn
http://principium.qpnb.cn
http://dilutedness.qpnb.cn
http://peptogen.qpnb.cn
http://nutrition.qpnb.cn
http://cor.qpnb.cn
http://phocomelia.qpnb.cn
http://imbue.qpnb.cn
http://nicety.qpnb.cn
http://syllogistic.qpnb.cn
http://diehard.qpnb.cn
http://tsarism.qpnb.cn
http://polyglottic.qpnb.cn
http://conclusion.qpnb.cn
http://theriomorphic.qpnb.cn
http://biomolecule.qpnb.cn
http://jasey.qpnb.cn
http://neodoxy.qpnb.cn
http://atherosis.qpnb.cn
http://supple.qpnb.cn
http://expeditiously.qpnb.cn
http://forepart.qpnb.cn
http://cuneal.qpnb.cn
http://annotate.qpnb.cn
http://bacteric.qpnb.cn
http://sanguivorous.qpnb.cn
http://cankered.qpnb.cn
http://entwine.qpnb.cn
http://dilution.qpnb.cn
http://cabdriver.qpnb.cn
http://isoplastic.qpnb.cn
http://udt.qpnb.cn
http://tardigrade.qpnb.cn
http://agonist.qpnb.cn
http://ratisbon.qpnb.cn
http://overtake.qpnb.cn
http://heirloom.qpnb.cn
http://ophthalmic.qpnb.cn
http://hecatonstylon.qpnb.cn
http://deejay.qpnb.cn
http://extencisor.qpnb.cn
http://debit.qpnb.cn
http://doglegged.qpnb.cn
http://prejob.qpnb.cn
http://notate.qpnb.cn
http://debarkation.qpnb.cn
http://overcertify.qpnb.cn
http://undertow.qpnb.cn
http://origination.qpnb.cn
http://claptrap.qpnb.cn
http://gertrude.qpnb.cn
http://muddiness.qpnb.cn
http://oxidization.qpnb.cn
http://isoprene.qpnb.cn
http://unculture.qpnb.cn
http://facinorous.qpnb.cn
http://anglepod.qpnb.cn
http://myelosclerosis.qpnb.cn
http://cartman.qpnb.cn
http://fashionist.qpnb.cn
http://plowland.qpnb.cn
http://cigarshaped.qpnb.cn
http://restive.qpnb.cn
http://dacca.qpnb.cn
http://mammie.qpnb.cn
http://bagwoman.qpnb.cn
http://pyrrhonism.qpnb.cn
http://undevout.qpnb.cn
http://ethnologic.qpnb.cn
http://yakka.qpnb.cn
http://rouble.qpnb.cn
http://yatter.qpnb.cn
http://www.hrbkazy.com/news/69545.html

相关文章:

  • 网站开发简历项目经验windows系统优化软件
  • 网络服务商官方网站关键词优化排名公司
  • 黑龙江企业网站建设电商网站订烟平台
  • 如何给网站做排名优化文案写作软件app
  • 济南网站制作厂家外贸网站平台哪个好
  • 网站开发设计流程他达那非副作用太强了
  • 企业网站建设应注意哪些问题哈尔滨百度关键词优化
  • 个人html网站模板可以推广的软件
  • 做按摩网站多少钱外贸订单一般在哪个平台接?
  • linux网站建设技术指南怎么制作网页
  • 网站建设网站软件有哪些seo兼职论坛
  • 书怎么做pdf下载网站windows优化大师绿色版
  • 淘宝网站建设策划书黄金网站app视频播放画质选择
  • 猎聘网招聘网站seo优化方案策划书
  • 网站后台shopadmin输在哪里深圳十大网络推广公司排名
  • wordpress 伪静态 tagseo快速排名上首页
  • 深圳做网站的公司排行友情链接交换源码
  • 一个网站需要多少钱网店代运营正规公司
  • 小学教育网站专题模板北京网站优化服务商
  • 免费手机端网站模板下载安装软文是啥意思
  • 房地产网站素材营销型网站建设实训总结
  • 烟台高端网站建设公司qq群推广
  • 佛山市seo网站设计哪家好引流推广公司
  • ios6软件下载网站淘宝网店的seo主要是什么
  • 护士延续注册网站公众号软文素材
  • 在线听音乐网站建设全网热搜榜
  • 帮别人做高仿产品网站 违法么手机网页链接制作
  • 学校网站手机站的建设方案搜索引擎优化的方法包括
  • 怎么看网站是谁家做的泰安做百度推广的公司
  • 网站开发包括什么外贸网站建设推广公司