【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 ...
随机推荐
- CF1149A Prefix Sum Primes
思路: 质数一定是奇数.实现: #include <bits/stdc++.h> using namespace std; int main() { int n, t, x, y; whi ...
- iOS 多尺寸屏幕适配
Point Point可以理解为iOS程序员眼中的大小单位.它是iOS操作系统中的抽象的概念. Rendered Pixels可以理解为UI设计师眼中的大小单位. Physical Pixels 设备 ...
- Perl 输出内容到 excel
可以参考: http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm 使用Spre ...
- jquery最常用的几个方法。——可删除
jquery使用手册:http://www.eduyo.com/doc/jquery/cheatsheet.html $(this).hasClass("input_money") ...
- vertx从入门到精通
1.Vert.x安装指南 http://blog.csdn.net/sdyy321/article/details/38926005 http://blog.csdn.net/chszs/articl ...
- 刷新本地DNS缓存的方法
http://www.cnblogs.com/rubylouvre/archive/2012/08/31/2665859.html 常有人问到域名解析了不是即时生效的嘛,怎么还是原来的呢?答案就是在本 ...
- OpenGL列主元矩阵和列主序存储
OpenGL矩阵要考虑两个点,一个是向量如何排布,一个是矩阵如何存储和恢复. 1.排布 排布决定了运算的顺序.OpenGL使用的是列主元,它的意思就是一个4X4的矩阵是由4个列向量构成(这里的v1,v ...
- 在vue组件库中不能使用v-for
没事的,有点时候编辑器报错,但运行不一定出错, 在vue组件中注意template标签
- chrom浏览器-F12使用方法二
文摘摘自:https://blog.csdn.net/run65536/article/details/80568543 提示:右键点击图片选择在新窗口或新标签页中打开可查看大图. 一.Element ...
- CPP-STL:STL备忘
STL备忘(转) 1. string.empty() 不是用来清空字符串,而是判断string是否为空,清空使用string.clear(); 2. string.find等查找的结果要和string ...