【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 ...
随机推荐
- 基于.NET网页开发的工作,需要掌握的知识点
学习计划对于程序员来说尤为重要,我最近根据自己的职业规划和招聘网站上对于基于.NET网页开发工作所需要的技能做出了一个总结,这个总结的内容也将是自己最近一年的知识补充和学习的方向,各位园友也可以把它作 ...
- Java中IO流文件读取、写入和复制
//构造文件File类 File f=new File(fileName); //判断是否为目录 f.isDirectory(); //获取目录下的文件名 String[] fileName=f.li ...
- WEB 前端菜鸟,感觉很迷茫,该怎么做?
前几天看到这样的问题 先说问题吧:感觉前端涉及到的东西太多了,自己也很浮躁,看了挺多书,可是代码缺敲得却不多.技术菜,又什么都想学,比如现在纠结要不要先学scss或者php或者angularjs,ba ...
- 提高VS2010运行速度的技巧+关闭拼写检查
任务管理器,CPU和内存都不高,为何?原因就是VS2010不停地读硬盘导致的; 写代码2/3的时间都耗在卡上了,太难受了; 研究发现,VS2010如果你装了VC等语言,那么它就会自动装SQL Serv ...
- gcc&g++
原文章 误区一:gcc只能编译c代码,g++只能编译c++代码两者都可以,但是请注意:1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序:后缀为.cpp的,两者都会认为是c++程序,注 ...
- Java MVC 分页实例
共4个文件 requestLogList.jsp RequestInfoController.java RequestInfoBean.java RequestInfoService.java 1.r ...
- vs2010调试sql2008存储过程
1.安装vs2010sp1补丁 2.vs中打开服务器资源管理器,并进行数据库连接,连接时要注意 3. 4.可以打开数据库中的存储过程进行调试了
- CentOS7.2+MySQL5.7_ yum源方式_ 安装配置教程
1)访问mysql官方网站 #访问网站 https://dev.mysql.com/downloads/file/?id=470281 2)下载安装包到linux #进入文件存放路径 cd /usr/ ...
- ZOJ 3537 Cake (区间DP,三角形剖分)
题意: 给出平面直角坐标系上的n个点的坐标,表示一个多边形蛋糕,先判断是否是凸多边形,若否,输出"I can't cut.".若是,则对这个蛋糕进行3角形剖分,切n-3次变成n-2 ...
- asp.net mvc集成log4net
第一步:在web项目的引用中添加log4net.dll,可以通过Nuget直接下载并安装: 第二步:在web项目的web.config配置文件的configuration节点内添加log4net节点, ...