[Leetcode]设计链表
题目#
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 库
代码#
typedef struct DoubleList{
int val;
DoubleList* pre;
DoubleList* next;
DoubleList(int value):val(value),pre(nullptr),next(nullptr){}
};
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList(): _head(new DoubleList(0)) {
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
auto ptr=_head;
while(ptr->next!=nullptr)
{
if(index==0)
{
return ptr->next->val;
}
index--;
ptr=ptr->next;
}
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) {
auto ptr=new DoubleList(val);
ptr->next=_head->next;
if(_head->next!=nullptr)
_head->next->pre=ptr;
_head->next=ptr;
ptr->pre=_head;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
auto ptr=_head;
while(ptr->next!=nullptr)
ptr=ptr->next;
ptr->next=new DoubleList(val);
ptr->next->pre=ptr;
}
/** 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) {
auto ptr=_head;
while(ptr->next!=nullptr)
{
if(index==0)
{
auto newNode=new DoubleList(val);
newNode->next=ptr->next;
ptr->next=newNode;
newNode->pre=ptr;
if(newNode->next!=nullptr)
{
newNode->next->pre=newNode;
}
return ;
}
index--;
ptr=ptr->next;
}
if(index==0)
{
auto newNode=new DoubleList(val);
newNode->pre=ptr;
ptr->next=newNode;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
auto ptr=_head;
while(ptr->next!=nullptr)
{
if(index==0)
{
auto del=ptr->next;
ptr->next=del->next;
if(del->next!=nullptr)
{
del->next->pre=ptr;
}
delete del;
return;
}
index--;
ptr=ptr->next;
}
}
private:
DoubleList* const _head;
};
/**
* 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);
*/
作者:lizhenghao126
出处:https://www.cnblogs.com/lizhenghao126/p/11053563.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
https://github.com/li-zheng-hao
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)