Leetcode707.Design Linked List设计链表
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 库。
struct Node
{
int val;
Node* next;
Node(int x) : val(x), next(NULL)
{
}
};
class MyLinkedList {
public:
Node *head;
int cnt;
MyLinkedList() {
head = NULL;
cnt = 0;
}
int get(int index) {
if(index < 0 || index > cnt - 1)
return -1;
Node* temp = head;
while(index)
{
temp = temp ->next;
index--;
}
return temp ->val;
}
void addAtHead(int val)
{
Node* node = new Node(val);
if(cnt == 0)
{
head = node;
}
else
{
node ->next = head;
head = node;
}
cnt++;
}
void addAtTail(int val)
{
Node* node = new Node(val);
if(cnt == 0)
{
head = node;
}
else
{
Node *temp = head;
while(temp ->next != NULL)
{
temp = temp ->next;
}
temp ->next = node;
}
cnt++;
}
void addAtIndex(int index, int val) {
if(index < 0 || index > cnt)
return;
if(index == 0)
{
addAtHead(val);
}
else if(index == cnt)
{
addAtTail(val);
}
else
{
Node* temp = head;
Node* node = new Node(val);
while(index > 1)
{
index--;
temp = temp ->next;
}
node ->next = temp ->next;
temp ->next = node;
cnt++;
}
}
void deleteAtIndex(int index) {
if(index < 0 || index >= cnt)
return;
Node* temp = head;
if(index == cnt - 1)
{
while(index > 1)
{
index--;
temp = temp ->next;
}
Node* clear = temp ->next;
free(clear);
temp ->next = NULL;
}
else
{
while(index > 1)
{
index--;
temp = temp ->next;
}
Node* clear = temp ->next;
Node* next = temp ->next ->next;
free(clear);
temp ->next = next;
}
cnt--;
}
};
Leetcode707.Design Linked List设计链表的更多相关文章
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...
- [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 是指向下一个节点的指针/ ...
- C#LeetCode刷题之#707-设计链表(Design Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...
- LeetCode707 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- 707. Design Linked List
1. 原始题目 Design your implementation of the linked list. You can choose to use the singly linked list ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
随机推荐
- windows修改或删除已保存samba输入的用户名和密码
可在系统的"控制面板\用户帐户\凭据管理器\windows 凭据"中找到 可以在"开始菜单->运行",输入"control userpasswo ...
- ROS 日志消息(C++)
1.日志级别 日志消息分为五个不同的严重级别宏,与Android的Log定义的严重级别类似,如下基础宏: ROS_DEBUG_STREAM.ROS_INFO_STREAM.ROS_WARN_STREA ...
- Ionic 微信支付
1.安装插件 ionic plugin add https://github.com/mrwutong/cordova-qdc-wxpay.git 2.代码 controller.js angular ...
- utils04_搭建私有Git服务器
1.远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改.GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给G ...
- 原型模式(Prototype)(对象、克隆广告邮件)
有些对象创建过程较为复杂,而且有些时候需要频繁的创建,原型模式通过给出一个原型对象来指明所要创建的对象的类型,然后复制这个原型对象的方法创建更多同类型的对象.这就是原型模式的动机. 原型模式的主要思想 ...
- apache支持多主机头,并防止恶意空主机头的配置实现
首先,需要启用 LoadModule vhost_alias_module modules/mod_vhost_alias.so # Virtual hostsInclude conf/extra/h ...
- 知道了为什么osg::impostor可以这样设置geometry的QUADS了
之前一直不理解为什么osg::impostor里面的impostorSprite可以直接设置impostorSprite->getCoords()来设置geometry的四个边角,其实是因为这个 ...
- 查看cpu性能和磁盘空间
df -h查看当前磁盘空间 du -sh查看当前目录占用的磁盘空间 du -sh * 查看当前所有目录占用的磁盘空间 lscpu查看cpu信息 free查看空间总量
- Spring学习:程序的耦合和解耦的思路分析
程序的耦合 耦合:程序间的依赖关系 包括: 类之间的依赖 方法间的依赖 解耦: 降低程序间的依赖关系 在实际开发中: 应该做到,编译期不依赖,运行时才依赖 解耦思路: 第一步:使用反射来创建对象,而避 ...
- Docker(一)简介及核心概念
1.简介 Docker是一个开源的应用容器引擎:是一个轻量级容器技术: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使用这个镜像: 运行中的这 ...