当前位置:网站首页>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;}}
边栏推荐
猜你喜欢
随机推荐
tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)
数学之美 第六章——信息的度量和作用
matplotlib画polygon, circle
leetcode 326. 3 的幂
若依集成browscap读取浏览器用户代理
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
不知道这4种缓存模式,敢说懂缓存吗?
Go语言类型与接口的关系
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
php根据两点经纬度计算距离
leetcode 1837. K 进制表示下的各位数字总和
Node version switching tool NVM and npm source manager nrm
Anaconda 虚拟环境迁移
盲埋孔PCB叠孔设计的利与弊
软件测试基本流程有哪些?权威的第三方软件检测机构推荐
Internet Download Manager简介及下载安装包,IDM序列号注册问题解决方法
PHP according to the longitude and latitude calculated distance two points
危化企业双重预防机制数字化建设进入全面实施阶段
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
【微信小程序2】事件传参与数据同步[03]