【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 ...
随机推荐
- LessCss学习笔记
一.入门 1.LESSCSS是什么? LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便CSS的编 ...
- jQuery测试及解析
解析:下标从0开始 解析:最大119 解析:鼠标移过mouseover 解析: var 变量值=变量名
- SQL server事务语法
ALTER proc [dbo].[p_BOGetMCBSecurityCheckPropertiesTypeAdd]@Name nvarchar(50), ---参数@MCBBadlyBuil ...
- Java基础50题test2—输出素数
[输出素数] 题目:判断 101-200 之间有多少个素数,并输出所有素数. 程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数 pu ...
- OutOfMemory
查看图片格式,如果为PNG,可更改为jpg.图片会变小. 停止activity 当activity调用onStop()方法, activity不再可见,并且应该释放那些不再需要的所有资源.一旦acti ...
- vijos 1524 最小监视代价
背景 看到Vijos上此类型的题目较少,特地放一道上来给大家练练. 描述 由于yxy小朋友做了一些不该做的事,他被jzp关进了一个迷宫里.由于jzp最近比较忙,疏忽大意了一些,yxy可以在迷宫中任意走 ...
- (转)MyBatis框架的学习(六)——MyBatis整合Spring
http://blog.csdn.net/yerenyuan_pku/article/details/71904315 本文将手把手教你如何使用MyBatis整合Spring,这儿,我本人使用的MyB ...
- ndarray数组自动创建
为了实现某些运算,需要快速构造符合要求的大数组 Numpy函数生成的数组,如不指定类类型,几乎全为浮点型(arange除外,它是整形),因为科学计算中测量值,例如温度.长度,都是浮点数 import ...
- 项目中多条数据保存的json实例
//js代码function checkCode(num){ var typeid = $("#typeid").val(); if(typeid == "") ...
- 开源 java 电商系统
shop++是基于spring.springmvc等主流框架开发,参考资料比较全面,上手容易: 比 javashop 代码可读性好. 适合二次开发 6.broadleaf基于spring.Spring ...