LeetCode707 设计链表
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 库。
//章节 - 链表
//一、单链表/三、双链表
//1.设计链表
/*
算法思想:一个常规方法,
1.坐标取结点函数,先判定index是否合法,然后从表头向后移动index个位置,找到要返回的结点即可。
2.增加表头函数就比较简单了,新建一个头结点,next连上head,然后head重新指向这个新结点,同时size自增1。
3.同样,对于增加表尾结点函数,首先遍历到表尾,然后在之后连上一个新建的结点,同时size自增1。
4.根据位置来加结点,那么肯定还是先来判定index是否合法,然后再处理一个corner case,就是当index为0的时候,直接调用前面的表头加结点函数即可。然后就是往后遍历index-1个结点,这里为啥要减1呢,因为要加入结点的话,必须要知道加入位置前面一个结点才行,最后size还是要自增1。
5.根据位置来删除结点,将cur的下一个结点指向cur的下下个结点,即实现删除。
*/
//算法实现:
/**
* 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);
*/
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList() {
head = NULL;
size = 0;
} /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) { //获取链表中第 index 个节点的值。如果索引无效,则返回-1。
if (index < 0 || index >= size)
return -1;
Node *cur = head;
for (int i = 0; i < index; ++i) //从表头向后移动index个位置
cur = cur->next;
return cur->val;
} /** 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) { //在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
Node *t = new Node(val, head); //新建一个头结点,next连上head
head = t; //head重新指向这个新结点
++size;
} /** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) { //将值为 val 的节点追加到链表的最后一个元素。
Node *cur = head;
while (cur->next) //遍历到表尾
cur = cur->next;
cur->next = new Node(val, NULL);
++size;
} /** 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) { /*在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。*/
if (index < 0 || index > size)
return;
if (index == 0) {addAtHead(val); return;} //
if (index == size) {addAtTail(val); return;} //
Node *cur = head;
for (int i = 0; i < index - 1; ++i)
cur = cur->next;
Node *t = new Node(val, cur->next); //t指向cur的下一个结点
cur->next = t; //cur的下一个结点指向t,注意,这两步顺序不能颠倒
++size; } /** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) { //如果索引 index 有效,则删除链表中的第 index 个节点。
if (index < 0 || index >= size)
return;
if (index == 0) { //删除头结点
head = head->next;
--size;
return;
}
Node *cur = head;
for (int i = 0; i < index - 1; ++i)
cur = cur->next;
cur->next = cur->next->next; //将cur的下一个结点指向cur的下下个结点,即实现删除
--size;
}
private:
struct Node { //结点定义
int val;
Node *next;
Node(int x, Node* n): val(x), next(n) {}
};
Node *head, *tail; //头、尾指针
int size; //链表大小
}; /*
算法思想:一个巧方法,
利用内置的双向队列deque这个数据结构实现这些操作。
*/
//算法实现:
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList() {} /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
return (index >= 0 && index < data.size()) ? data[index] : -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) {
data.push_front(val);
} /** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
data.push_back(val);
} /** 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) {
if (index < 0 || index > data.size())
return;
data.insert(data.begin() + index, val);
} /** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if (index < 0 || index >= data.size())
return;
data.erase(data.begin() + index);
} private:
deque<int> data;
};
LeetCode707 设计链表的更多相关文章
- [Swift]LeetCode707. 设计链表 | 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
爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...
- Leetcode707.Design Linked List设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- LeetCode | 707. 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- Java实现 LeetCode 707 设计链表(环形链表)
707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- LeetCode 707 ——设计链表
1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...
- LeetCode——142 设计链表2
题目 代码 class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head, *slow ...
随机推荐
- 前端js实现九宫格模式抽奖(多宫格抽奖)
介绍: 前端九宫格是一种常见的抽奖方式,js实现如下,掌握其原理,不论多少宫格,都可以轻松应对.(代码可复制直接运行看效果). 该案例以四宫格入门,可扩展多宫格,奖品模块的布局可自由设置. <! ...
- Notepad++ 使用步骤,熟练掌握notepad++的使用技巧,无疑会大大提升专业技能。以及快捷键操作
官方下载地址: https://notepad-plus.en.softonic.com/ 1.安装 双击安装包出现以下界面 2.点击我接受 3.安装地址 继续下一步 4.默认即可,继续下一步 5.根 ...
- Jmeter(9)常用定时器
测试计划中元件的执行顺序依次为: 配置元件--逻辑控制器--前置处理器--定时器--取样器--后置处理器--断言--监听器 一.定时器作用域 1.定时器是在每个取样器之前执行的,无论定时器是在取样器之 ...
- 2. 使用Shell能做什么
批处理 在批处理的过程中,能够实现脚步自动化,比GUI自动化速度高效 日常工作场景 服务端测试 移动端测试 持续集成与自动化部署,这是最最场景的场景,可以说离开了shell,持续集成和自动化部署也会遇 ...
- Java读取系统默认时区
工作中,遇到一个Java读取默认时区的问题,后来看了openjdk的源码,大致整理一下过程 public class Test { public void test(){ TimeZone.getDe ...
- 工具-Redis-使用(99.6.2)
@ 目录 1.启动 2.数据结构 3.String命令 4.其他常用命令 5.Hash命令 6.List命令 7.Set命令 8.Zset命令 关于作者 1.启动 redis-server 交互 re ...
- Python求一个数字列表的元素总和
Python求一个数字列表的元素总和.练手: 第一种方法,直接sum(list): 1 lst = list(range(1,11)) #创建一个1-10的数字列表 2 total = 0 #初始化总 ...
- win7激活不支持的启动引导分区完美解决方法
前言: 激活win7显示不支持的启动引导分区怎么办?有用户使用暴风激活工具给win7 64位系统激活时,弹出Error提示框"不支持的启动引导分区". 这是因为传统的win7激活工 ...
- (四)、vim的缓冲区、标签、窗口操作
1.缓冲区的基本操作 a.文件与缓冲区的区别 vim file1 打开一个文件时,其实是从磁盘中读取文件到内存中,文件的内容会被加载到缓冲区中, 这个缓冲区在一个窗口上显示,所以他是一个已激活的缓 ...
- Vue2+Koa2+Typescript前后端框架教程--03后端路由和三层模式配置
昨天将Koa2的基础框架和自动编译调试重启服务完成,今天开始配置路由和搭建基础的三层架构模式. 路由中间件:koa-router,即路由导航,就是我们平时使用最广泛的get/post方法执行的URL路 ...