当前位置:网站首页>leetcode 707.设计链表
leetcode 707.设计链表
2022-04-21 06:37:00 【小学五年级在读的蒟蒻】
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 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
题解
设计思路一开始没想到,后面看了会题解就就明白了,基本上都是用一个新变量来不断遍历寻找插入删除的位置
/* * @lc app=leetcode.cn id=707 lang=cpp * * [707] 设计链表 */
// @lc code=start
class MyLinkedList
{
//构造链表的结构体
struct Listnode
{
int val;
Listnode *next;
Listnode(int x) : val(x), next(nullptr) {
}
};
Listnode *vir;
Listnode *cur;
public:
MyLinkedList()
{
vir = new Listnode(0);
cur = vir;
}
int get(int index)
{
//遍历节点
Listnode *tmp = vir->next;
if (!tmp)
return -1;
while (index)
{
tmp = tmp->next;
if (!tmp)
return -1;
index--;
}
return tmp->val;
}
void addAtHead(int val)
{
//将头节点的后面节点抓出
//创建新节点将需要插入的值初始化 并将next的指向改成刚抓出的节点
Listnode *tmp = new Listnode(val);
Listnode *Head = vir->next;
vir->next = tmp;
tmp->next = Head;
}
void addAtTail(int val)
{
Listnode *node = new Listnode(val);
Listnode *tmp = vir;
while (tmp->next)
{
tmp = tmp->next;
/* code */
}
tmp->next = node;
}
void addAtIndex(int index, int val)
{
if (index <= 0)
{
addAtHead(val);
return;
}
Listnode *tmp = vir;
while (index)
{
tmp = tmp->next;
if (!tmp)
{
return;
}
index--;
/* code */
}
Listnode *insert = new Listnode(val);
insert->next = tmp->next;
tmp->next = insert;
return;
}
void deleteAtIndex(int index)
{
Listnode *tmp = vir;
while (index)
{
tmp = tmp->next;
if (!tmp)
{
return;
}
index--;
/* code */
}
Listnode *node = tmp->next;
if (!node)
{
return;
}
Listnode *next = node->next;
tmp->next = next;
delete node;
return;
}
};
/** * 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); */
// @lc code=end
版权声明
本文为[小学五年级在读的蒟蒻]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44229867/article/details/122398985
边栏推荐
- PIM-DM
- SMS logic vulnerability
- Cs5801 specification | cs5801 HDMI to EDP conversion scheme | HDMI to DP adapter board design, hdmi2 0 turn edp1 4. Support downward compatibility
- 2020-12-25
- DOS command kills process
- On balanced binary tree
- Implementation of crud of user management system with JDBC
- 【WPF】VisualStateManager
- TCP三次握手和四次挥手简介(2022.4.18-4.24)
- 2022-1-17至2022-1-30 迭代器模式
猜你喜欢

命令模式(3.21-3.27)

Clock IC, ins5101a, I2C low-power RTC real-time clock chip, replace hym8563, replace (NXP) PCF8563, tcs8563, I2C low-power RTC real-time clock chip

栈(C语言)

Cjc5340, ADC, DAC, replacement cs5340, digital to analog converter, 100db192khz multi bit audio A / D converter, audio codec100db192khz multi bit audio A / D converter

Login interface universal password bypass

解释器模式(3.7-3.13)

2022.2.14-2.27 责任链模式

Sorting method (1) = > bubble sorting, simple selection sorting

NP、OSPF Stub區域

IGMP_华为
随机推荐
数据库死锁总结:(3.7-3.13)
Optimal solution of steel bar cutting problem
論文閱讀:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
Ruiyuan ry8132 and ry9140 DCDC are mainly replaced by tps563200 and tps563201 of Ti, mp1471 and mp1653 of Xinyuan, and details of sy8104 power chip of silijie
2020-12-24
NP、OSPF 故障排除
Define a standard class
NP、OSPF 缺省路由
IGMP_ Huawei
Mini examination system v1 Version 0.0
栈(C语言)
Blue Bridge Cup - palindromes and special palindromes
论文阅读:Security Challenges in an Increasingly Tangled Web
【计组】冯诺依曼体系结构
论文阅读:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
IP组播基本概念
Three layer switch and router are connected to the Internet
Sort method ----- > Hill sort, heap sort
论文阅读:Measuring the Global Recursive DNS Infrastructure: A View From the Edge
DOS command kills process