当前位置:网站首页>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;}}
边栏推荐
- php根据两点经纬度计算距离
- 不知道这4种缓存模式,敢说懂缓存吗?
- 622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
- Detailed explanation of JWT
- 涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
- 抖音web逆向教程
- 面试官:为什么 0.1 + 0.2 == 0.300000004?
- 利用 rpush 和 blpop 实现 Redis 消息队列
- glide set gif start stop
- Detailed steps for tensorflow-gpu2.4.1 installation and configuration
猜你喜欢
Detailed AST abstract syntax tree
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
In-depth understanding of JVM-memory structure
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
头条服务端一面经典10道面试题解析
【STM32】标准库-自定义BootLoader
宁德时代2号人物黄世霖辞任副董事长:身价1370亿
随机推荐
力扣206-反转链表——链表
谁的孙子最多II
ARMuseum
李沐动手学深度学习V2-自然语言推断与数据集SNLI和代码实现
ES6解构赋值--数组解构及对象解构
codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
【STM32】标准库-自定义BootLoader
消除对特权账户的依赖使用Kaniko构建镜像
(十六)51单片机——红外遥控
leetcode 268. 丢失的数字(异或!!)
Anaconda virtual environment migration
php根据两点经纬度计算距离
Why BI software can't handle correlation analysis
ThreadLocal详解
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
Hinton2022年RobotBrains访谈记录
力扣203-移除链表元素——链表
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法
高位套牢机构,用友网络的信任危机是如何产生的?
利用 rpush 和 blpop 实现 Redis 消息队列