[Leetcode]设计链表
题目
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
- get(index):获取链表中第
index个节点的值。如果索引无效,则返回-1。 - addAtHead(val):在链表的第一个元素之前添加一个值为
val的节点。插入后,新节点将成为链表的第一个节点。 - addAtTail(val):将值为
val的节点追加到链表的最后一个元素。 - addAtIndex(index,val):在链表中的第
index个节点之前添加值为val的节点。如果index等于链表的长度,则该节点将附加到链表的末尾。如果index大于链表长度,则不会插入节点。 - deleteAtIndex(index):如果索引
index有效,则删除链表中的第index个节点。
示例:
MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3
提示:
- 所有值都在
[1, 1000]之内。 - 操作次数将在
[1, 1000]之内。 - 请不要使用内置的 LinkedList 库
代码
typedef struct DoubleList{
int val;
DoubleList* pre;
DoubleList* next;
DoubleList(int value):val(value),pre(nullptr),next(nullptr){}
};
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList(): _head(new DoubleList(0)) {
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
auto ptr=_head;
while(ptr->next!=nullptr)
{
if(index==0)
{
return ptr->next->val;
}
index--;
ptr=ptr->next;
}
return -1;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void addAtHead(int val) {
auto ptr=new DoubleList(val);
ptr->next=_head->next;
if(_head->next!=nullptr)
_head->next->pre=ptr;
_head->next=ptr;
ptr->pre=_head;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
auto ptr=_head;
while(ptr->next!=nullptr)
ptr=ptr->next;
ptr->next=new DoubleList(val);
ptr->next->pre=ptr;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
auto ptr=_head;
while(ptr->next!=nullptr)
{
if(index==0)
{
auto newNode=new DoubleList(val);
newNode->next=ptr->next;
ptr->next=newNode;
newNode->pre=ptr;
if(newNode->next!=nullptr)
{
newNode->next->pre=newNode;
}
return ;
}
index--;
ptr=ptr->next;
}
if(index==0)
{
auto newNode=new DoubleList(val);
newNode->pre=ptr;
ptr->next=newNode;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
auto ptr=_head;
while(ptr->next!=nullptr)
{
if(index==0)
{
auto del=ptr->next;
ptr->next=del->next;
if(del->next!=nullptr)
{
del->next->pre=ptr;
}
delete del;
return;
}
index--;
ptr=ptr->next;
}
}
private:
DoubleList* const _head;
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
[Leetcode]设计链表的更多相关文章
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- LeetCode | 707. 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- Java实现 LeetCode 707 设计链表(环形链表)
707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...
- [Swift]LeetCode707. 设计链表 | Design Linked List
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- LeetCode707:设计链表 Design Linked List
爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...
- Leetcode解题-链表(2.2.0)基础类
1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø 规定好每个子Solution都要实现纯虚函数test做测试: Ø 提供了List ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- Leetcode707.Design Linked List设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- LeetCode707 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
随机推荐
- javascript编程单线程之异步模式Asynchronous
异步模式Asynchronous 不会等待这个任务结束才开始执行下一个任务,开启之后立即执行下一个任务,后续逻辑一般会通过回调函数的方式定义,异步模式对js 非常重要,没有异步任务单线程的 js 语言 ...
- 五、Python操作redis
五.Python操作redis 一.python对redis基本操作 (1)连接redis # 方式1 import redis r = redis.Redis(host='127.0.0.1', p ...
- Ubuntu实现电商网站+Mysql主从复制+NFS
Ubuntu实现电商网站+Mysql主从复制+NFS 1.环境准备 提前准备:Mysql8.0.30安装包.Mysql安装脚本.shopxo2.3.0安装包.DNS脚本 服务器 IP地址 作用 系统版 ...
- 2流高手速成记(之八):基于Sentinel实现微服务体系下的限流与熔断
我们接上回 上一篇中,我们进行了简要的微服务实现,也体会到了SpringCloudAlibaba的强大和神奇之处 我们仅改动了两个注释,其他全篇代码不变,原来的独立服务就被我们分为了provider和 ...
- java学习之SpringMVC
0x00前言 Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架,本质上相当于 Servlet. Spring MVC 是结构最清晰的 Servlet+ ...
- [leetcode] 994. Rotting Oranges
题目 You are given an m x n grid where each cell can have one of three values: 0 representing an empty ...
- mysql删库报错
3.开发人员测试环境删库报错 #解决:在数据库的物理目录中(mysql的data目录),进入要删除的数据库目录,查看是否有文件存在,若存在,使用rm -rf 命令清除:再次执行删除数据库命令即可 [r ...
- 基于 MQ 的分布式 Serverless 多租任务处理系统架构演进
本文作者:史明伟 , 阿里云智能高级技术专家. 1 Serverless 异步任务处理系统诞生和挑战 无论是对于云的开发者,还是尝试业务升级的企业客户,Serverless的三个概念 "极致 ...
- Go语言核心36讲22
你好,我是郝林,今天我们继续来分享错误处理. 在上一篇文章中,我们主要讨论的是从使用者的角度看"怎样处理好错误值".那么,接下来我们需要关注的,就是站在建造者的角度,去关心&quo ...
- ifconfig命令的使用
ifconfig命令 用途:配置或显示TCP/IP网络的网络接口参数. *1.通过--help学习ifconfig的使用 点击查看代码 [root@rocky8 ~]# ifconfig --help ...