LeetCode 707. 设计链表(List)

文章目录

    • 1. 设计一个单链表
    • 2. 双向链表

1. 设计一个单链表

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class node
{
public:int val;node *next;node(int v):val(v),next(NULL) {}
};
class MyLinkedList {node *head, *tail;int len;
public:/** Initialize your data structure here. */MyLinkedList() {head = tail = NULL;len = 0;}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */int get(int index) {if(index >= len || index < 0)return -1;node *cur = head;while(index--)cur = cur->next;return cur->val;}/** 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) {  node *h = new node(val);h->next = head;head = h;if(len == 0)tail = head;++len;}/** Append a node of value val to the last element of the linked list. */void addAtTail(int val) {node *t = new node(val);if(len == 0){head = tail = t;}else{tail->next = t;tail = t;}++len; }/** 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) {if(index == len)addAtTail(val);else if(index <= 0)addAtHead(val);else if(index > len)return;else{node *cur = head;while(--index)cur = cur->next;node *newNode = new node(val);newNode->next = cur->next;cur->next = newNode;++len;}}/** Delete the index-th node in the linked list, if the index is valid. */void deleteAtIndex(int index) {if(index >= len || index < 0)return;--len;node *virtualheadNode, *del, *cur;virtualheadNode = new node(0);virtualheadNode->next = head;cur = virtualheadNode;while(index--){cur = cur->next;}del = cur->next;cur->next = cur->next->next;delete del;head = virtualheadNode->next;if(cur->next == NULL)tail = cur;delete virtualheadNode;}
};

在这里插入图片描述

2. 双向链表

class node
{
public:int val;node *next;node *prev;node(int v):val(v),next(NULL),prev(NULL) {}
};
class MyLinkedList {node *head, *tail;int len;
public:/** Initialize your data structure here. */MyLinkedList() {head = tail = NULL;len = 0;}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */int get(int index) {if(index >= len || index < 0)return -1;node *cur = head;while(index--)cur = cur->next;return cur->val;}/** 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) {  node *h = new node(val);h->next = head;head = h;if(len == 0)tail = head;++len;}/** Append a node of value val to the last element of the linked list. */void addAtTail(int val) {node *t = new node(val);if(len == 0){head = tail = t;}else{tail->next = t;t->prev = tail;tail = t;}++len; }/** 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) {if(index == len)addAtTail(val);else if(index <= 0)addAtHead(val);else if(index > len)return;else{node *cur = head;while(--index)cur = cur->next;node *newNode = new node(val);newNode->next = cur->next;newNode->prev = cur;cur->next->prev = newNode;cur->next = newNode;++len;}}/** Delete the index-th node in the linked list, if the index is valid. */void deleteAtIndex(int index) {if(index >= len || index < 0)return;--len;node *virtualheadNode, *del, *cur;virtualheadNode = new node(0);virtualheadNode->next = head;head->prev = virtualheadNode;cur = virtualheadNode;while(index--){cur = cur->next;}del = cur->next;cur->next = cur->next->next;if(del->next) del->next->prev = cur;delete del;head = virtualheadNode->next;if(cur->next == NULL)tail = cur;delete virtualheadNode;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/478669.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用Vue.js开发微信小程序:开源框架mpvue解析

前言 mpvue 是一款使用 Vue.js 开发微信小程序的前端框架。使用此框架&#xff0c;开发者将得到完整的 Vue.js 开发体验&#xff0c;同时为 H5 和小程序提供了代码复用的能力。如果想将 H5 项目改造为小程序&#xff0c;或开发小程序后希望将其转换为 H5&#xff0c;mpvue 将是…

对比学习有多火?文本聚类都被刷爆了…

文 | 花小花Posy大家好&#xff0c;我是小花。对比学习的大火???? 越来越旺了&#xff0c;已然从CV蔓延到NLP了。今天给大家介绍的正是一篇将对比学习应用到文本聚类上的工作&#xff0c;NAACL21新鲜出炉的paper——《Supporting Clustering with Contrastive Learning》。…

论文浅尝 - WWW2020 | 生成多跳推理问题以改善机器阅读理解能力

论文笔记整理&#xff1a;谭亦鸣&#xff0c;东南大学博士生。来源&#xff1a;WWW 2020链接&#xff1a;https://dl.acm.org/doi/pdf/10.1145/3366423.3380114概述这篇论文关注的任务是&#xff1a;基于给定文本的“多跳问题生成”&#xff08;多关系问题&#xff09;。作者提…

记一次Vue框架升级

框架升级背景 公司目前业务迭代很快&#xff0c;且大部分的流量都在公众号上。然而我们公众号所使用的框架却是3年前的Vue 1.0.16。面对Vue这3年来带来的无数新特性&#xff0c;我们只能望洋兴叹&#xff1a;看得见&#xff0c;摸不着&#xff0c;因为升级这事看起来太难了。 …

谈谈NLP下一个主战场:万亿参数的预训练模型!

自从BERT诞生以来&#xff0c;各大互联网巨头之间就展开了预训练语言模型军备竞赛&#xff0c;XLNet、ERNIE、RoBERTa、T5、GPT-3....但当事情进展到号称自己是zero-shot learner的GPT-3时&#xff0c;批判的声音变得明显多了。这么大&#xff0c;能用吗&#xff1f;真的能做到…

人物志 | 美团女技术总监任登君:不要给自己的人生设限

在我们美团技术团队超过6000名工程师中&#xff0c;有众多的女同学&#xff0c;她们是支撑中国领先的生活服务电子商务平台不可或缺的力量。3月8日女神节&#xff0c;我们专访了她们的代表——美团广告平台技术负责人任登君。登君也是我们团队里目前职位最高的女性技术Leader&a…

论文浅尝 - ISWC2020 | KnowlyBERT: 知识图谱结合语言模型补全图谱查询

论文笔记整理&#xff1a;胡楠&#xff0c;东南大学博士。来源&#xff1a;ISWC 2020动机像Wikidata这样的现代知识图已经捕获了数十亿个RDF三元组&#xff0c;但是它们仍然缺乏对大多数关系的良好覆盖。同时在NLP研究的最新进展表明&#xff0c;可以轻松地查询神经语言模型以获…

LeetCode 92. 反转链表 II(双指针)

1. 题目 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 说明: 1 ≤ m ≤ n ≤ 链表长度。 示例:输入: 1->2->3->4->5->NULL, m 2, n 4 输出: 1->4->3->2->5->NULL来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 链接&#xf…

我对你的爱,是只为你而留的神经元

文 | 白鹡鸰有一个小轶专属神经元编 | 小轶有一个白鹡鸰专属神经元什么是苹果&#xff1f;红的&#xff1f;绿的&#xff1f;黄的&#xff1f;球状&#xff1f;斑点&#xff1f;香气&#xff1f;需要咬上一口才能确定&#xff1f;或者……其实我们在说某家技术公司&#xff1f;…

Android动态日志系统Holmes

背景 美团是全球领先的一站式生活服务平台&#xff0c;为6亿多消费者和超过450万优质商户提供连接线上线下的电子商务网络。美团的业务覆盖了超过200个丰富品类和2800个城区县网络&#xff0c;在餐饮、外卖、酒店旅游、丽人、家庭、休闲娱乐等领域具有领先的市场地位。平台大&a…

领域应用 | 知识图谱在小米的应用与探索

本文转载自公众号&#xff1a;DataFunTalk。分享嘉宾&#xff1a;彭力 小米编辑整理&#xff1a;马瑶出品平台&#xff1a;DataFunTalk导读&#xff1a;小米知识图谱于2017年创立&#xff0c;已支持公司了每天亿级的访问&#xff0c;已赋能小爱同学&#xff0c;小米有品、智能问…

前端应用开发架构图谱

个人整理的前端架构图谱&#xff0c;之后会根据这个图谱不断的完善内容。希望这个图谱可以对开发同学的知识脉络有个梳理的作用。 相关图谱文件已上传至Github&#xff1a;https://github.com/sahadev/front-end-architecture&#xff0c;后续将不定期更新。 2020年02月28日已…

丹琦女神新作:对比学习,简单到只需要Dropout两下

文 | 花小花Posy上周把 《对比学习有多火&#xff1f;文本聚类都被刷爆了...》分享到卖萌屋的群里后&#xff0c;遭到了群友们一波嫌弃安利。小伙伴们表示&#xff0c;插入替换的数据增强方式已经Out了&#xff0c;SimCSE才是现在的靓仔。snowfloating说&#xff1a;看完Danqi …

美团点评移动端基础日志库——Logan

背景 对于移动应用来说&#xff0c;日志库是必不可少的基础设施&#xff0c;美团点评集团旗下移动应用每天产生的众多种类的日志数据已经达到几十亿量级。为了解决日志模块普遍存在的效率、安全性、丢失日志等问题&#xff0c;Logan基础日志库应运而生。 现存问题 目前&#xf…

论文浅尝 - ACL2020 | Segmented Embedding of Knowledge Graphs

来源&#xff1a;ACL2020链接&#xff1a;https://arxiv.org/pdf/2005.00856.pdf摘要知识图谱的嵌入愈发变成AI的热点之一&#xff0c;对许多下游任务至关重要&#xff08;如个性化推荐、问答等&#xff09;同时&#xff0c;此模型强调两个关键特性&#xff1a;利用足够多的特征…

【论文翻译】HeteSim:异构网络中相关性度量的通用框架

原文链接&#xff1a;https://blog.csdn.net/Mrong1013967/article/details/115330139 HeteSim&#xff1a;异构网络中相关性度量的通用框架 摘要 相似性搜索是许多应用中的一个重要功能&#xff0c;它通常侧重于度量同一类型对象之间的相似性。然而&#xff0c;在许多场景中&a…

LeetCode 234. 回文链表(快慢指针+链表反转)

1. 题目 请判断一个链表是否为回文链表。 示例 1: 输入: 1->2 输出: false示例 2: 输入: 1->2->2->1 输出: true进阶&#xff1a; 你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题&#xff1f;来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 链接&a…

随机/线性颜色生成器(RandomColorGenerator)

最近在实现https://javascript30.com/的课程&#xff0c;其中有一门课程要求利用Canvas实现一个效果&#xff0c;我发现这个效果其中的颜色是线性生成的。结合我之前已经写过一个随机颜色生成器&#xff0c;就想将这个随机颜色生成器写出来&#xff0c;作为一个工具使用&#x…

美团点评运营数据产品化应用与实践

背景 美团点评作为全球最大的生活服务平台&#xff0c;承接超过千万的POI&#xff0c;服务于数量庞大的活跃用户。在海量数据的前提下&#xff0c;定位运营业务、准确找到需要数据的位置&#xff0c;并快速提供正确、一致、易读的数据就变得异常困难&#xff0c;这些困难主要体…

NAACL’21 | 来看如何让模型学会因为所以但是如果

文 | Eleanor 编 | 戏有一些标准考试那是真的难&#xff0c;难到能分分钟教你做人。对于留学党来说&#xff0c;申请法学博士需要 LSAT 考试成绩、申请商学院需要 GMAT 考试成绩。这些标准考试到底有多难&#xff0c;大概考过的都懂8&#xff08;嘤嘤嘤_(:з」∠)_&#xff09;…