【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题。这是在学习链表时碰见的。
题目要求:
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:
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 库。
题目都说的很详细了,直接给代码吧。
题目代码:
class MyLinkedList {
private:
struct ListNode{
int val;
ListNode *next;
};
ListNode *linkedList;
public:
/** Initialize your data structure here. */
MyLinkedList() {
linkedList=NULL;
}
/** 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<0)
return -1;
ListNode *p=linkedList;
int i=0;
while(p!=NULL&&i<index){
i++;
p=p->next;
}
if(i==index&&p!=NULL)
return p->val;
else
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) {
ListNode *head=(ListNode*)malloc(sizeof(ListNode));
head->val=val;
head->next=linkedList;
linkedList=head;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
ListNode *p=linkedList;
ListNode *tail=(ListNode*)malloc(sizeof(ListNode));
tail->val=val;
tail->next=NULL;
if(!p)
linkedList=tail;
while(p->next){
p=p->next;
}
p->next=tail;
}
/** 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) {
ListNode *p;
ListNode *add=(ListNode*)malloc(sizeof(ListNode));
int i=0;
add->val=val;
add->next=NULL;
if(!linkedList&&!index)
linkedList=add;
else{
p=linkedList;
while(p&&index-1>i){
i++;
p=p->next;
}
if(index=i+1&&p){
add->next=p->next;
p->next=add;
}
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if(!linkedList)
return;
if(!index){
ListNode *p=linkedList;
linkedList=linkedList->next;
free(p);
}
else{
int i=0;
ListNode *p=linkedList;
while(i<index-1){
i++;
p=p->next;
}
if(i+1==index&&p->next){
ListNode *q=p->next;
p->next=q->next;
free(q);
}
}
}
};
/**
* 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);
*/
提交结果:
个人总结:
很多处地方的while条件可以改为p=p->next,简化代码。
【LeetCode】Design Linked List(设计链表)的更多相关文章
- [LeetCode] 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设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...
- [LeetCode] 141. Linked List Cycle 链表中的环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- [LeetCode] Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- [LeetCode] Design Phone Directory 设计电话目录
Design a Phone Directory which supports the following operations: get: Provide a number which is not ...
- [LeetCode] Design Hit Counter 设计点击计数器
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
随机推荐
- ReferenceError: password is not defined
报错提示位置at c:\Users\Administrator\WebstormProjects\blogtest\routes\index.js:19:16 原因是我这个password没有定义,p ...
- Java环境安装与Eclipse安装
1.jdk下载安装 2.Eclipse下载安装 遇到的问题: 出现问题原因可能有两个:1)没有配置环境变量 2)jdk和eclipse安装的版本不一致,都是64位或者都是32位. 本人出现错误的原因: ...
- React 实践记录 02 Flux introduction
Introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出. 一旦稍微多了解一点React,很难避免听到Flux这个名词. Flux是一 ...
- 安装ubuntu虚拟环境
一. 安装 1. 准备: 1). Oracle VM VirtualBox https://www.virtualbox.org/ 2). Ubuntu 18.04.2 LTS https://ubu ...
- 从零开始利用vue-cli搭建简单音乐网站(八)
这是完成了预想中的最后两个功能:歌曲评论以及歌曲搜索. 1.评论效果: 用户点击评论按钮,评论框获取焦点. 输入之后点击提交,下方显示评论,用户名称以及日期.相应的用户也可以删除自己评论. 当然只能删 ...
- redis 一些使用过的命令
因为我是JAVA的,所以也是用java的api 主要是文档看起来太麻烦,自己英文也不好,每次用之前都要看一遍,自己把常用的一点点的放进来,方便使用 分布式连接池对象配置 JedisPoolConfig ...
- How to install Eclipse in linux
http://askubuntu.com/questions/26632/how-to-install-eclipse
- The Jaisalmer Desert Festival 2017/2/9
原文 India's Golden City celebrates its culture with costumes(服装),crafts(工艺品) and camels One of the fe ...
- (十一)maven之安装nexus私服
安装nexus私服 前面的文章中对项目引入jar依赖包的时候,maven一般先是在本地仓库找对应版本的jar依赖包,如果在本地仓库中找不到,就上中央仓库中下载到本地仓库. 然而maven默认提供的中央 ...
- 关于自动化测试环境的集成(Jenkins+RobotFramework+TestLink+SVN)
本人主要从事网络安全产品的测试,由于一些产品功能在后期稳定后每个版本的迭代仍需要投入大量的时间和精力去测试,所以近期计划逐步的去了解自动化测试的一些内容来节省和解放一些资源.由于自己并没有什么编码基础 ...