当前位置:网站首页>Likou 707 - Design Linked List - Linked List
Likou 707 - Design Linked List - Linked List
2022-08-03 20:11:00 【Zhang Ran Ran √】
Title description
Design the implementation of linked lists.You can choose to use a singly or doubly linked list.A node in a singly linked list should have two properties: val and next.val is the value of the current node and next is a pointer/reference to the next node.If you want to use a doubly linked list, you also need an attribute prev to indicate the previous node in the linked list.Assume that all nodes in the linked list are 0-indexed.
Implement these functions in the linked list class:
get(index): Get the value of the index-th node in the linked list.Returns -1 if the index is invalid.
addAtHead(val): Add a node with a value of val before the first element of the linked list.After insertion, the new node will be the first node of the linked list.
addAtTail(val): Append the node with value val to the last element of the linked list.
addAtIndex(index,val): Add a node with a value of val before the index-th node in the linked list.If index is equal to the length of the linked list, the node will be appended to the end of the linked list.If index is greater than the length of the linked list, the node will not be inserted.If index is less than 0, insert the node at the head.
deleteAtIndex(index): If the index index is valid, delete the index-th node in the linked list.
Solution ideas
AddAtTail(val) and addAtHead(val) can both be implemented by the function addAtIndex(index,val).
Input and output example

Code
class ListNode{int val;ListNode next;ListNode(){}ListNode(int val){this.val = val;}}class MyLinkedList {int size;ListNode head;public MyLinkedList() {size = 0;head = new ListNode(0);}//Get the index-th nodepublic int get(int index) {if(index < 0 || index >= size){return -1;}ListNode cur = head;for(int i = 0; i <= index; i++){cur = cur.next;}return cur.val;}//Insert a node at the top of the linked listpublic void addAtHead(int val) {addAtIndex(0,val);}//Insert a node at the last position of the linked listpublic void addAtTail(int val) {addAtIndex(size,val);}//Insert node val at indexpublic void addAtIndex(int index, int val) {if(index > size) return;if(index < 0) index = 0;size++;ListNode pre = head;for(int i = 0; i < index; i++){pre = pre.next;}ListNode toAdd = new ListNode(val);toAdd.next = pre.next;pre.next = toAdd;}// delete the index nodepublic void deleteAtIndex(int index) {if(index < 0 || index >= size) return;ListNode pre = head;size--;for(int i = 0; i < index; i++){pre = pre.next;}pre.next = pre.next.next;}}边栏推荐
- 友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
- 自定义form表单验证
- codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
- RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
- abs()、fabs() 和 labs() 的区别
- 边缘盒子+时序数据库,美的数字化平台 iBuilding 背后的技术选型
- 简易电子琴设计(c语言)
- 模板字符串概述
- 双线性插值公式推导及Matlab实现
- 染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
猜你喜欢
随机推荐
嵌入式分享合集27
Auto.js脚本程序打包
机器学习中专业术语的个人理解与总结(纯小白)
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
JMeter笔记5 |Badboy使用和录制
收藏-即时通讯(IM)开源项目OpenIM-功能手册
leetcode 125. 验证回文串
Anaconda virtual environment migration
高位套牢机构,用友网络的信任危机是如何产生的?
charles配置客户端请求全部不走缓存
力扣203-移除链表元素——链表
ES6简介及let、var、const区别
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
alicloud3搭建wordpress
LeetCode 1374. 生成每种字符都是奇数个的字符串
盲埋孔PCB叠孔设计的利与弊
详解AST抽象语法树
RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405
危化企业双重预防机制数字化建设进入全面实施阶段
一种能有效缓解环境噪声对音频质量干扰的方案









