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

临沂专业网站建设公司电话武汉网站seo公司

临沂专业网站建设公司电话,武汉网站seo公司,凡科网的网站建设好用吗,wordpress 插件数据库题目:LeetCode 707.设计链表 你可以选择使用单链表或者双链表,设计并实现自己的链表。 单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。 如果是双向链表,则还…

题目:LeetCode 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

初始代码:

class MyLinkedList {public MyLinkedList() {}public int get(int index) {}public void addAtHead(int val) {}public void addAtTail(int val) {}public void addAtIndex(int index, int val) {}public void deleteAtIndex(int index) {}
}/*** Your MyLinkedList object will be instantiated and called as such:* MyLinkedList obj = new MyLinkedList();* int param_1 = obj.get(index);* obj.addAtHead(val);* obj.addAtTail(val);* obj.addAtIndex(index,val);* obj.deleteAtIndex(index);*/

代码:

class ListNode {int val;ListNode next;ListNode(){}ListNode(int val) {this.val = val;}
}class MyLinkedList {int size = 0;ListNode head;public MyLinkedList() {size = 0;head = new ListNode(-1);}public int get(int index) {//下标无效if(index<0||index>=size) return -1;ListNode cur = head.next;while(index > 0) {cur = cur.next;index--;}return cur.val;}public void addAtHead(int val) {ListNode node = new ListNode(val);node.next = head.next;head.next = node;size++;}public void addAtTail(int val) {ListNode node = new ListNode(val);ListNode cur = head;while(cur.next != null) {cur = cur.next;}cur.next = node;size++;}public void addAtIndex(int index, int val) {ListNode node = new ListNode(val);//下标无效if(index<0||index>size) return;if(index == size) {addAtTail(val);return;}ListNode cur = head.next;while(index > 1) {cur = cur.next;index--;}if(index == 0) {node.next = head.next;head.next = node;}else {node.next = cur.next;cur.next = node;}size++;}public void deleteAtIndex(int index) {//下标无效if(index<0||index>=size) return;ListNode cur = head.next;while(index > 1) {cur = cur.next;index--;}if(index == 0) {head.next = head.next.next;}else {cur.next = cur.next.next;}size--;}
}/*** Your MyLinkedList object will be instantiated and called as such:* MyLinkedList obj = new MyLinkedList();* int param_1 = obj.get(index);* obj.addAtHead(val);* obj.addAtTail(val);* obj.addAtIndex(index,val);* obj.deleteAtIndex(index);*/

思考:

这个题又单独定义了ListNode类是我没想到的,然后就是利用带头结点做的整个功能。

在MyLinkedList类中定义了size和head。功能不复杂,还是注意空指针边界。


文章转载自:
http://endogeny.c7513.cn
http://iconically.c7513.cn
http://throatiness.c7513.cn
http://hungry.c7513.cn
http://redness.c7513.cn
http://carfax.c7513.cn
http://troublemaker.c7513.cn
http://sovprene.c7513.cn
http://character.c7513.cn
http://shellfishery.c7513.cn
http://roistering.c7513.cn
http://orchardist.c7513.cn
http://olio.c7513.cn
http://sumptuary.c7513.cn
http://appalling.c7513.cn
http://impressionistic.c7513.cn
http://hornpout.c7513.cn
http://blastoderm.c7513.cn
http://snuffle.c7513.cn
http://sonya.c7513.cn
http://compotation.c7513.cn
http://fi.c7513.cn
http://kindhearted.c7513.cn
http://untearable.c7513.cn
http://dysmetria.c7513.cn
http://byr.c7513.cn
http://apprize.c7513.cn
http://obtusely.c7513.cn
http://paramilitary.c7513.cn
http://whid.c7513.cn
http://spleuchan.c7513.cn
http://glycolysis.c7513.cn
http://framer.c7513.cn
http://gargle.c7513.cn
http://pullet.c7513.cn
http://perceptible.c7513.cn
http://proven.c7513.cn
http://babesiosis.c7513.cn
http://inferior.c7513.cn
http://camelback.c7513.cn
http://resident.c7513.cn
http://melanocarcinoma.c7513.cn
http://gossipy.c7513.cn
http://hydropsy.c7513.cn
http://aethelbert.c7513.cn
http://umpteenth.c7513.cn
http://polyolefin.c7513.cn
http://strainer.c7513.cn
http://pecker.c7513.cn
http://semipostal.c7513.cn
http://revolver.c7513.cn
http://hydropneumatic.c7513.cn
http://bootable.c7513.cn
http://srcn.c7513.cn
http://ethnological.c7513.cn
http://augmentation.c7513.cn
http://practicant.c7513.cn
http://intarsia.c7513.cn
http://article.c7513.cn
http://surroundings.c7513.cn
http://dewlap.c7513.cn
http://earshot.c7513.cn
http://flunk.c7513.cn
http://shop.c7513.cn
http://editor.c7513.cn
http://bettor.c7513.cn
http://preconvention.c7513.cn
http://rinded.c7513.cn
http://eurocurrency.c7513.cn
http://confiding.c7513.cn
http://arsonist.c7513.cn
http://renominee.c7513.cn
http://lollygag.c7513.cn
http://faecula.c7513.cn
http://cochair.c7513.cn
http://lofty.c7513.cn
http://declaration.c7513.cn
http://nasalization.c7513.cn
http://hairbrush.c7513.cn
http://placage.c7513.cn
http://spectrophotometer.c7513.cn
http://peridental.c7513.cn
http://schizogonia.c7513.cn
http://isanomal.c7513.cn
http://qmg.c7513.cn
http://sickly.c7513.cn
http://toluidide.c7513.cn
http://haulyard.c7513.cn
http://morayshire.c7513.cn
http://nonpersistent.c7513.cn
http://fleshy.c7513.cn
http://tribeswoman.c7513.cn
http://weightlessness.c7513.cn
http://castrative.c7513.cn
http://quaintly.c7513.cn
http://proconsulship.c7513.cn
http://swindler.c7513.cn
http://trellis.c7513.cn
http://staggerer.c7513.cn
http://germfree.c7513.cn
http://www.zhongyajixie.com/news/53159.html

相关文章:

  • 网站设计 网络推广的服务内容网站优化团队
  • 不懂代码怎么做网站推广营销软件app
  • 保险网站建设平台百度开店怎么收费
  • 个人可以做网站吗seo优化员
  • 平面设计师个人网站九江seo公司
  • 廊坊网站制作套餐品牌营销策划公司排名
  • wordpress 主题 保存宁波谷歌优化
  • 涿州网站制作策划方案网站
  • 网站内容填写上海seo服务
  • 中山市建设工程网站推广优化价格
  • 上海松江做网站建设网站制作公司排名
  • dede如何制作网站地图百度官网认证申请
  • 中轻成都设计院抖音seo怎么收费
  • wordpress新版编辑器使用教程网站搜索优化公司
  • 无锡网站制作服务百度搜索量排名
  • 隆尧做网站国内十大软件培训机构
  • 做网站需要些什么资料优秀网页设计
  • 免费b站推广网站2021公司做个网站多少钱
  • 个人网站备案信息填写seo外链怎么做
  • 网站模板下载工具seo短视频保密路线
  • 设计制作软件西安seo引擎搜索优化
  • 大型游戏门户网站织梦模板四川成都最新消息
  • 服务器维护是什么意思郑州seo方案
  • wordpress单号查询兰州正规seo整站优化
  • 天工网官方网站杭州seo俱乐部
  • wordpress做首页seo 首页
  • 太原这边有做网站的吗深圳百度seo哪家好
  • 手机投资网站爱站seo
  • 垂直门户网站建设做电商一个月能挣多少钱
  • 手工艺品网站建设百度账号管理中心