【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 ...
随机推荐
- Java——HashSet和TreeSet的区别
HashSetHashSet有以下特点 不能保证元素的排列顺序,顺序有可能发生变化 不是同步的 集合元素可以是null,但只能放入一个null当向HashSet集合中存入一个元素时,HashSe ...
- Struct和Class的区别 转
C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能.struct能包含成员函数吗? 能!struct能继承吗? 能!!stru ...
- Android 8.0 NotificationChannel 采坑实例
Android O 上Notification的新特性: 通知通道功能 1. 简介: 通知通道功能使开发者管理自己应用的通知成为一个组或者一个通道,用户可以通过通知通道完成设置通知,如:阻止所有通知, ...
- 使用 Azure 创建存储和检索文件
本指南将以循序渐进的方式帮助您使用 Azure 将文件存储到云中.我们将逐一介绍如何创建存储账户.创建容器.上传文件.检索文件和删除文件.在本教程中完成的所有操作均符合 1 元试用条件. 本指南将以循 ...
- 【Web应用-网络连接】Azure Web 应用对外连接数上限分析
在 Azure Web 应用中发起大量外部连接操作时,需要考虑已经建立了多少外部连接.当超过最大对外连接数时,Azure Web 应用将会产生套接字异常.Azure Web 应用对于各个级别的实例,对 ...
- python super详解
一.super() 的入门使用 - 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能, 这时,我们就需要调用父类的方法了,可通过使用 super 来实 ...
- Git服务器和Git权限管理应用GITLAB安装方法
首先声明,本文使用的服务器是Centos 6.5,在其他版本的LINUX上运行不保证也是一样的效果. 顺便说下 来波点赞 来波收藏和推荐 有什么问题 我会一直关注评论的 想放一张最终图吧 其中主要涉 ...
- python中的循环语句-01
接触python已经一周时间,下面针对这一周python学习的情况做一梳理: 1)单行注释,使用一个#:多行注释,使用三个单引号(或者三个双引号)如: name = input("name: ...
- Spring启动流程—源码解读
https://blog.csdn.net/yangliuhbhd/article/details/80790761 Spring的AbstractApplicationContext的refresh ...
- 基于Passthru的NDIS开发的个人理解
这几天对NDIS的学习,基本思路是:首先熟悉理论知识→然后下载一个例子进行研究→最后例子自己模仿扩展→最最后尝试自己写一个新的. Passthru是微软NDIS自己写的一个框架驱动,NDIS开发者可以 ...