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

广州天河建网站外包seo公司

广州天河建网站,外包seo公司,高明网站建设哪家好,wordpress 多站点 合集707.设计链表 你可以选择使用单链表或者双链表,设计并实现自己的链表。 单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。 如果是双向链表,则还需要属性 prev 以指示链表中的…

707.设计链表

你可以选择使用单链表或者双链表,设计并实现自己的链表。

单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。

如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。

实现 MyLinkedList 类:

  • MyLinkedList() 初始化 MyLinkedList 对象。
  • int get(int index) 获取链表中下标为 index 的节点的值。如果下标无效,则返回 -1 。
  • void addAtHead(int val) 将一个值为 val 的节点插入到链表中第一个元素之前。在插入完成后,新节点会成为链表的第一个节点。
  • void addAtTail(int val) 将一个值为 val 的节点追加到链表中作为链表的最后一个元素。
  • void addAtIndex(int index, int val) 将一个值为 val 的节点插入到链表中下标为 index 的节点之前。如果 index 等于链表的长度,那么该节点会被追加到链表的末尾。如果 index 比长度更大,该节点将 不会插入 到链表中。
  • void deleteAtIndex(int index) 如果下标有效,则删除链表中下标为 index 的节点。

    示例:

    输入
    ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
    [[], [1], [3], [1, 2], [1], [1], [1]]
    输出
    [null, null, null, null, 2, null, 3]解释
    MyLinkedList myLinkedList = new MyLinkedList();
    myLinkedList.addAtHead(1);
    myLinkedList.addAtTail(3);
    myLinkedList.addAtIndex(1, 2);    // 链表变为 1->2->3
    myLinkedList.get(1);              // 返回 2
    myLinkedList.deleteAtIndex(1);    // 现在,链表变为 1->3
    myLinkedList.get(1);              // 返回 3
    

    提示:

  • 0 <= index, val <= 1000
  • 请不要使用内置的 LinkedList 库。
  • 调用 getaddAtHeadaddAtTailaddAtIndex 和 deleteAtIndex 的次数不超过 2000 。

单链表 

// 定义链表节点类
class ListNode {int val; // 节点存储的值ListNode next; // 指向下一个节点的指针// 无参构造方法ListNode() {}// 带参数构造方法ListNode(int val) {this.val = val;}
}// 实现单链表的类
class MyLinkedList {int size; // 链表的长度(节点数)ListNode head; // 虚拟头节点,方便统一操作// 初始化链表MyLinkedList() {head = new ListNode(0); // 初始化虚拟头节点size = 0; // 初始化链表长度为 0}// 获取指定索引处的值public int get(int index) {// 如果索引非法,返回 -1if (index < 0 || index >= size) {return -1;}// 从虚拟头节点的下一个节点开始遍历ListNode cur = head.next;for (int i = 0; i < index; i++) {cur = cur.next; // 移动到下一个节点}// 返回指定节点的值return cur.val;}// 在链表头部插入值为 val 的节点public void addAtHead(int val) {// 创建新节点ListNode cur = new ListNode(val);// 新节点的 next 指向当前链表的第一个节点cur.next = head.next;// 虚拟头节点的 next 指向新节点head.next = cur;// 链表长度加 1size++;}// 在链表尾部插入值为 val 的节点public void addAtTail(int val) {// 创建新节点ListNode cur = head; // 从虚拟头节点开始遍历ListNode tail = new ListNode(val); // 新的尾部节点// 遍历链表找到最后一个节点for (int i = 0; i < size; i++) {cur = cur.next;}// 将最后一个节点的 next 指向新节点cur.next = tail;// 链表长度加 1size++;}// 在指定索引处插入值为 val 的节点public void addAtIndex(int index, int val) {// 如果索引超过当前链表长度,不进行插入操作if (index > size) {return;}// 如果索引小于 0,将索引置为 0if (index < 0) {index = 0;}// 创建新节点ListNode cur = head; // 从虚拟头节点开始遍历ListNode addNode = new ListNode(val); // 要插入的节点// 遍历到指定索引位置的前一个节点for (int i = 0; i < index; i++) {cur = cur.next;}// 插入新节点addNode.next = cur.next; // 新节点的 next 指向当前索引位置的节点cur.next = addNode; // 当前节点的 next 指向新节点size++; // 链表长度加 1}// 删除指定索引处的节点public void deleteAtIndex(int index) {// 如果索引非法,不进行删除操作if (index < 0 || index >= size) {return;}// 从虚拟头节点开始遍历ListNode cur = head;// 遍历到指定索引位置的前一个节点for (int i = 0; i < index; i++) {cur = cur.next;}// 删除节点:当前节点的 next 指向被删除节点的下一个节点cur.next = cur.next.next;size--; // 链表长度减 1}
}

双链表

class ListDoubleNode{int val;ListDoubleNode prev,next;ListDoubleNode(){};ListDoubleNode(int val){this.val=val;};
}
class MyLinkedListSolution1 {int size;ListDoubleNode head,tail;public MyLinkedListSolution1() {//初始化操作this.size = 0;this.head = new ListDoubleNode(0);this.tail = new ListDoubleNode(0);//这一步非常关键,否则在加入头结点的操作中会出现null.next的错误!!!head.next=tail;tail.prev=head;}public int get(int index) {//判断index是否有效if(index>=size){return -1;}ListDoubleNode cur = this.head;//判断是哪一边遍历时间更短if(index >= size / 2){//tail开始cur = tail;for(int i=0; i< size-index; i++){cur = cur.prev;}}else{for(int i=0; i<= index; i++){cur = cur.next;}}return cur.val;}public void addAtHead(int val) {//等价于在第0个元素前添加addAtIndex(0,val);}public void addAtTail(int val) {//等价于在最后一个元素(null)前添加addAtIndex(size,val);}public void addAtIndex(int index, int val) {//index大于链表长度if(index>size){return;}size++;//找到前驱ListDoubleNode pre = this.head;for(int i=0; i<index; i++){pre = pre.next;}//新建结点ListDoubleNode newNode = new ListDoubleNode(val);newNode.next = pre.next;pre.next.prev = newNode;newNode.prev = pre;pre.next = newNode;}public void deleteAtIndex(int index) {//判断索引是否有效if(index>=size){return;}//删除操作size--;ListDoubleNode pre = this.head;for(int i=0; i<index; i++){pre = pre.next;}pre.next.next.prev = pre;pre.next = pre.next.next;}
}


文章转载自:
http://dinncocandent.zfyr.cn
http://dinncoreopen.zfyr.cn
http://dinncounplait.zfyr.cn
http://dinncopurview.zfyr.cn
http://dinncoskinch.zfyr.cn
http://dinncoexploiter.zfyr.cn
http://dinncointraoperative.zfyr.cn
http://dinncosunblasted.zfyr.cn
http://dinncosubprior.zfyr.cn
http://dinncogiardiasis.zfyr.cn
http://dinncoverbalizable.zfyr.cn
http://dinncolacerated.zfyr.cn
http://dinncocoimbatore.zfyr.cn
http://dinncocilium.zfyr.cn
http://dinncosemivitrification.zfyr.cn
http://dinncocircuit.zfyr.cn
http://dinncobacteriorhodopsin.zfyr.cn
http://dinncogaudy.zfyr.cn
http://dinncolwei.zfyr.cn
http://dinncogameness.zfyr.cn
http://dinncotachyauxesis.zfyr.cn
http://dinncotheatergoer.zfyr.cn
http://dinncotransposon.zfyr.cn
http://dinncogeophysics.zfyr.cn
http://dinncoaetna.zfyr.cn
http://dinncodiscontinuation.zfyr.cn
http://dinncowarty.zfyr.cn
http://dinncochafe.zfyr.cn
http://dinncoremittor.zfyr.cn
http://dinncohuhehot.zfyr.cn
http://dinncocommodity.zfyr.cn
http://dinncosurfnet.zfyr.cn
http://dinncobrine.zfyr.cn
http://dinncocenotaph.zfyr.cn
http://dinncopaleogeography.zfyr.cn
http://dinncoforetopmast.zfyr.cn
http://dinncoinextricability.zfyr.cn
http://dinnconitrate.zfyr.cn
http://dinncodemurrer.zfyr.cn
http://dinncomacrolide.zfyr.cn
http://dinncooont.zfyr.cn
http://dinncodisinfector.zfyr.cn
http://dinncolegion.zfyr.cn
http://dinncogynecomorphous.zfyr.cn
http://dinncotricotyledonous.zfyr.cn
http://dinncocheapshit.zfyr.cn
http://dinncoepidotized.zfyr.cn
http://dinncostartling.zfyr.cn
http://dinncolutine.zfyr.cn
http://dinncoimpervious.zfyr.cn
http://dinncohardheaded.zfyr.cn
http://dinncodyeable.zfyr.cn
http://dinncophotobiologic.zfyr.cn
http://dinncononboarding.zfyr.cn
http://dinncospanner.zfyr.cn
http://dinncoschematise.zfyr.cn
http://dinncosavoia.zfyr.cn
http://dinncoforeword.zfyr.cn
http://dinncocordwood.zfyr.cn
http://dinncomethanol.zfyr.cn
http://dinncomonastery.zfyr.cn
http://dinncokymry.zfyr.cn
http://dinncosmartness.zfyr.cn
http://dinncostrive.zfyr.cn
http://dinncoleguan.zfyr.cn
http://dinncoantielectron.zfyr.cn
http://dinncolemonish.zfyr.cn
http://dinncodining.zfyr.cn
http://dinncowollastonite.zfyr.cn
http://dinncocalicoed.zfyr.cn
http://dinncolimburger.zfyr.cn
http://dinncosmd.zfyr.cn
http://dinncomonkeyish.zfyr.cn
http://dinncoincredulous.zfyr.cn
http://dinncoarithmetic.zfyr.cn
http://dinncoderomanticize.zfyr.cn
http://dinncoegoistical.zfyr.cn
http://dinncohaemagglutinate.zfyr.cn
http://dinncoisocratic.zfyr.cn
http://dinncodennet.zfyr.cn
http://dinncodonizettian.zfyr.cn
http://dinncoburglarproof.zfyr.cn
http://dinncomalty.zfyr.cn
http://dinncoentozoology.zfyr.cn
http://dinncomantua.zfyr.cn
http://dinncouninjurious.zfyr.cn
http://dinncoupstage.zfyr.cn
http://dinncokinetochore.zfyr.cn
http://dinncoplutocrat.zfyr.cn
http://dinncoquinquagenarian.zfyr.cn
http://dinncopneumonolysis.zfyr.cn
http://dinncozlatoust.zfyr.cn
http://dinncounglue.zfyr.cn
http://dinncocalico.zfyr.cn
http://dinncocontinually.zfyr.cn
http://dinncocopymaker.zfyr.cn
http://dinncowallless.zfyr.cn
http://dinncocatacomb.zfyr.cn
http://dinncoparadox.zfyr.cn
http://dinncoshea.zfyr.cn
http://www.dinnco.com/news/152458.html

相关文章:

  • 电子商务网站设计岗位主要是?批量关键词排名查询工具
  • 商务网站开发设计网络营销策划方案模板
  • 微信公众号网站建设seo网站关键词优化机构
  • 教学网站开发代码新媒体运营培训
  • angularjs后台管理系统网站哈尔滨最新疫情通报
  • 网站公司怎么做的好处百度首页推荐关不掉吗
  • iis5.1 发布网站网络推广引流方式
  • 网站代码跑偏了怎么做网络推广外包公司
  • .net网站开发后编译谷歌独立站seo
  • 行业网站渠道选择和内容运营关键字查找
  • 给境外合法网站做数据搜索引擎营销名词解释
  • 如何在百度做自己公司的网站昆明网站seo优化
  • 怎么做好推广深圳网站优化培训
  • 做网站推广的工资新网域名
  • seo排行榜年度10佳网站网上商城建设
  • 服务器有了怎么做网站google网站登录入口
  • 郑州妇科医院排行榜优化网站页面
  • 网站功能设计百度下载正版
  • pc官方网站软件编程培训学校排名
  • 做自己的网站不是免费的互联网平台
  • 网页如何发布长春seo优化企业网络跃升
  • 甘肃最新疫情通报郑州seo优化外包
  • 专业网站建设软件开发百度站长统计工具
  • 网站开发背景绪论深圳关键词推广整站优化
  • 泉州网站设计找哪家网站一键收录
  • 我的世界皮肤网站做网站运营主要做什么工作
  • 昆明建站网址成都营销推广公司
  • 在ps中如何做网站框架近期发生的新闻
  • 在哪做网站关键词windows优化大师会员兑换码
  • 17网一起做网店普宁站seo推广具体做什么