当前位置:网站首页>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;}}边栏推荐
- 调用EasyCVR云台控制接口时,因网络延迟导致云台操作异常该如何解决?
- 149. The largest number on a straight line, and check the set
- LeetCode 622. 设计循环队列
- leetcode 1837. The sum of the digits in the K-base representation
- 【STM32】标准库-自定义BootLoader
- Statistical machine learning 】 【 linear regression model
- MapReduce介绍及执行过程
- alicloud3搭建wordpress
- EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
- 双线性插值公式推导及Matlab实现
猜你喜欢

高位套牢机构,用友网络的信任危机是如何产生的?

ESP8266-Arduino编程实例-WS2812驱动

亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得

【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)

Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)

机器学习中专业术语的个人理解与总结(纯小白)

一种能有效缓解环境噪声对音频质量干扰的方案

倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即

Edge box + time series database, technology selection behind Midea's digital platform iBuilding

RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405
随机推荐
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
MySQL Basics
力扣59-螺旋矩阵 II——边界判断
宁德时代2号人物黄世霖辞任副董事长:身价1370亿
leetcode 125. 验证回文串
亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得
tensorflow-gpu2.4.1安装配置详细步骤
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
LeetCode 1374. 生成每种字符都是奇数个的字符串
多模态 参考资料汇总
leetcode 1837. The sum of the digits in the K-base representation
Node version switching tool NVM and npm source manager nrm
云服务器如何安全使用本地的AD/LDAP?
不知道这4种缓存模式,敢说懂缓存吗?
JMeter笔记5 |Badboy使用和录制
node版本切换工具NVM以及npm源管理器nrm
async 和 await 原来这么简单
codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
leetcode 1837. K 进制表示下的各位数字总和
622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)