当前位置:网站首页>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;}}边栏推荐
猜你喜欢

Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning

codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】

Hinton2022年RobotBrains访谈记录

百利药业IPO过会:扣非后年亏1.5亿 奥博资本是股东

async 和 await 原来这么简单

abs()、fabs() 和 labs() 的区别

leetcode 231. 2 的幂

RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405

力扣203-移除链表元素——链表

云服务器如何安全使用本地的AD/LDAP?
随机推荐
利用 rpush 和 blpop 实现 Redis 消息队列
leetcode 326. 3 的幂
List类的超详细解析!(超2w+字)
2022 年值得尝试的 7 个 MQTT 客户端工具
JMeter笔记5 |Badboy使用和录制
leetcode 268. 丢失的数字(异或!!)
Node version switching tool NVM and npm source manager nrm
【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
盲埋孔PCB叠孔设计的利与弊
ESP8266-Arduino编程实例-WS2812驱动
头条服务端一面经典10道面试题解析
YARN功能介绍、交互流程及调度策略
数据驱动的软件智能化开发| ChinaOSC
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
Hinton2022年RobotBrains访谈记录
简易电子琴设计(c语言)
软件测试基本流程有哪些?权威的第三方软件检测机构推荐
RNA核糖核酸修饰荧光染料|HiLyte Fluor 488/555/594/647/680/750标记RNA核糖核酸
Anaconda 虚拟环境迁移