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 ...
随机推荐
- HBase 表和Region
- Selenium浏览器自动化测试使用(1)
Selenium - 介绍 Selenium是一个开源的和便携式的自动化软件测试工具,用于测试Web应用程序有能力在不同的浏览器和操作系统运行.Selenium真的不是一个单一的工具,而是一套工具,帮 ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- Redis Replication & Sentinel
实践目标: Redis Replication 一主:192.168.1.104 双从:192.168.1.101 192.168.1.103 Sentinel:192.168.1.102 系统环境: ...
- PostgreSQL 优化器代码概览
简介 PostgreSQL 的开发源自上世纪80年代,它最初是 Michael Stonebraker 等人在美国国防部支持下创建的POSTGRE项目.上世纪末,Andrew Yu 等人在它上面搭建了 ...
- [JSOI2010]连通数 (dfs或tarjan或bitset)+bitset学习
题目描述 输入格式 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. 输出格式 输出一行一个整数,表示该图的连通数. 样例 样 ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--02--02CMDB将服务器基本信息提交到API接口
AutoCmdb # urls.py """AutoCmdb URL Configuration The `urlpatterns` list routes URLs t ...
- [转]模块化——Common规范及Node模块实现
Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于javascrip ...
- python 日记 day1
1.python2 与 python3 的区别: a. python2 源码不标准,混乱,重复代码太多.默认方式是ascii码,解决方式:#-*- encoding:utf-8 -*- b. ...
- OpenCASCADE动画功能
OpenCASCADE动画功能 eryar@163.com 1.Introduction OpenCASCADE提供了类AIS_Animation等来实现简单的动画功能. 从其类图可以看出,动画功能有 ...