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 ...
随机推荐
- Luogu P1486 [NOI2004]郁闷的出纳员(平衡树)
P1486 [NOI2004]郁闷的出纳员 题意 题目描述 \(OIER\)公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作 ...
- [转]浅谈C#中常见的委托
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- 爱上一门语言不需要理由——我的js之路
开始记录js学习:~~~~分享一下你的js学习途径吧 决定学习前端之后,开始接触JavaScript 1995年,网景公司的Brendan Eich用10天完成了JavaScript的设计,他被称为J ...
- .h头文件 .lib动态链接库文件 .dll 动态链接库
(1).h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib 不是.dll 若生成了DLL ,则肯定也生成 LIB文件 如果要完成源代码的编译和链接,有头文件和 ...
- Eclipse使用过程的常见问题:
3-1 "Failed to load the JNI shared library" -jdk 与eclipse位数不一致出现的问题 解决方法: ...
- keras multi-label classification 多标签分类
问题:一个数据又多个标签,一个样本数据多个类别中的某几类:比如一个病人的数据有多个疾病,一个文本有多种题材,所以标签就是: [1,0,0,0,1,0,1] 这种高维稀疏类型,如何计算分类准确率? 分类 ...
- Jquery 页面打印
<script src="~/Scripts/js/dist/jquery.jqprint-0.3.js"></script> <script typ ...
- python3没有了xrange
升级到python3的同学应该会注意到以前经常用的xrange没了! 是的,python3的range就是xrange.直接看效果! Python 2.7.13 (v2.7.13:a06454b1 ...
- Luogu P4011 孤岛营救问题(状态压缩+最短路)
P4011 孤岛营救问题 题意 题目描述 \(1944\)年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克得到 ...
- 使用Charles在iOS6上进行抓包
抓取Web页面的网络请求很容易,Chrome和Firefox都很容易做到.iOS APP如何抓包呢?其实也很容易,我比较喜欢使用 Charles. 我用的是Mac电脑,首先建立一个热点,然后让iOS设 ...