设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

示例:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3

题解

class MyLinkedList {private class Node{private int val;private Node next;private Node(int val, Node next) {this.val = val;this.next = next;}}private Node head;private Node rear;private int length;/** Initialize your data structure here. */public MyLinkedList() {head = new Node(0,null);rear = head;length = 0;}private Node getNode(int index){if(index == length-1){return rear;}else{Node node = head.next;for(int i = 0;i < index;i++){node = node.next;}return node;}}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */public int get(int index) {if(index < 0 || index > length - 1){return -1;}return getNode(index).val;}/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */public void addAtHead(int val) {Node node = new Node(val,null);if(length == 0){addAtTail(val);}else{Node temp = head.next;head.next = node;node.next = temp;length++;}}/** Append a node of value val to the last element of the linked list. */public void addAtTail(int val) {Node node = new Node(val,null);rear.next = node;rear = node;length++;}/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */public void addAtIndex(int index, int val) {if(index > length){return;}if(index <= 0){     //头部插入addAtHead(val);}else if(index == length){     //尾部插入addAtTail(val);}else{      //中间插入Node node = new Node(val,null);Node temp = getNode(index-1);node.next = temp.next;temp.next = node;length++;}}/** Delete the index-th node in the linked list, if the index is valid. */public void deleteAtIndex(int index) {if( !(index < 0 || index > length - 1) ){if(index == 0){Node node = head.next;head.next = node.next;node = null;}else if(index == length-1){Node node = getNode(length-2);node.next = null;rear = null;rear = node;}else{Node node = getNode(index-1);Node target = node.next;node.next = target.next;target = null;}length--;}}
}

leetcode 707 设计链表相关推荐

  1. LeetCode 707. 设计链表(List)

    文章目录 1. 设计一个单链表 2. 双向链表 1. 设计一个单链表 在链表类中实现这些功能: get(index):获取链表中第 index 个节点的值.如果索引无效,则返回-1. addAtHea ...

  2. Leetcode 707.设计链表

    传送门:力扣 答案在文后/ 在链表类中实现这些功能: get(index):获取链表中第 index 个节点的值.如果索引无效,则返回-1. addAtHead(val):在链表的第一个元素之前添加一 ...

  3. LeetCode刷题---707. 设计链表(双向链表-带头尾双结点)

    文章目录 一.编程题:707. 设计链表(双向链表-带头尾双结点) 1.题目描述 2.示例1: 3.提示: 二.解题思路 1.思路 2.复杂度分析: 3.算法图解(双向链表) 三.代码实现 三.单向链 ...

  4. 力扣707设计链表(单链表,JavaScript)

    1,头指针指向列表第一个元素,尾指针指向链表最后一个元素,链表末尾的index=this.size-1 var MyLinkedList = function() {this.size=0this.h ...

  5. Suzy找到实习了吗Day 3 | 链表开始啦 203移除链表元素 707设计链表 206 反转链表

    定义链表的结构 class ListNode:def __init__(self, val, next=None): #构造函数self.val = valself.next = next 尾部nod ...

  6. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

  7. day03链表基础_移除链表元素_设计链表_反转链表

    链表理论基础 链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思). 链表的入口节点称 ...

  8. My Seventh Page - 设计链表 - By Nicolas

    这一篇page对应的是leetcode上面707.设计链表这个题目,首先这个题目的描述还是比较简单的,就是我们可以选择使用单链表或者双链表实现链表对应的增删改查的许多功能.小尼这个题目大部分都是看的题 ...

  9. 设计链表(单链表、双链表)

    707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表 ...

最新文章

  1. MySQL探秘(五):InnoDB锁的类型和状态查询
  2. Linux下使用socket传输文件的C语言简单实现
  3. php数组有没有类似next方法,PHP 数组current跟next用法
  4. 并查集模板——并查集(洛谷 P3367)
  5. gtk_widget_modify_bg的用法
  6. 为什么四个字节的float表示的范围比八个字节的long要广
  7. java drawline_如何设置java drawLine画的线的粗细
  8. 计算机专业窗体的事件何时触发,高三计算机专业VB试题(七)
  9. 中标麒麟的下载和安装
  10. 【传智播客】Javaweb程序设计任务教程 黑马程序员 第一章 课后答案
  11. 利用CMD命令关闭进程
  12. 微软OneDrive使用体验
  13. 【工程光学】平面与平面系统
  14. SpringSecurity授权管理介绍
  15. 波段高低点指标公式 k线高低点 大盘主图公式源码
  16. 工作中的会议纪要模板
  17. 【JqGrid】JqGrid单元格合并及表头列合并,jqgrid单元格合并
  18. 春节不出门!这三款超好评编程游戏,好玩到停不下来
  19. 1、二进制安装k8s
  20. 创业公司如何搭建服务器配置方案?

热门文章

  1. 日历设计那点事,你知道多少?
  2. CSAPP:计算机基本结构与CPU内部构造
  3. 【学习笔记-FPGA】verilog语言中assign的使用
  4. 2016电大计算机网考,2016年电大-电大计算机网考试卷.doc
  5. windows office 界面改进(续)
  6. 上海交通大学 Old Bill(java)
  7. 安卓图片加载框架Gilde的用法
  8. iOS开发技巧之:如何用Xcode导出ipa包
  9. ftp连接服务器出现的问题(主动模式与被动模式)
  10. 如何降重计算机SCI论文的重复率? - 易智编译EaseEditing