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 个节点。
示例:
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
提示:
- 所有
val值都在$ [1, 1000] $之内。 - 操作次数将在 $[1, 1000] $之内。
- 请不要使用内置的
LinkedList库。
Code
struct myListNode
{
int val;
myListNode* next;
myListNode() :val(-1), next(nullptr) {}
};
class MyLinkedList {
public:
int m_size;
myListNode* m_head;
myListNode* m_tail;
public:
/** Initialize your data structure here. */
MyLinkedList() :m_size(0)
{
m_head = new myListNode();
}
/** 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 > m_size - 1 || index < 0)
{
return -1;
}
myListNode* p = m_head->next;
while (index != 0)
{
p = p->next;
--index;
}
//std::cout << p->val;
return p->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)
{
myListNode* newNode = new myListNode();
newNode->next = m_head->next;
m_head->next = newNode;
newNode->val = val;
m_size += 1;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val)
{
myListNode* lastNode = new myListNode();
lastNode->val = val;
myListNode* p = m_head;
int len = m_size;
while (len != 0)
{
p = p->next;
--len;
}
p->next = lastNode;
lastNode->next = NULL;
m_size += 1;
}
/** 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 > m_size) return;
if (index == m_size)
{
addAtTail(val);
return;
}
if (index < 0)
{
addAtHead(val);
return;
}
myListNode* newNode = new myListNode();
myListNode* p = m_head;
while (index != 0)
{
p = p->next;
--index;
}
newNode->next = p->next;
p->next = newNode;
newNode->val = val;
m_size += 1;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index)
{
if (index > m_size - 1 || index < 0) return;
myListNode* p = m_head;
while (index != 0)
{
p = p->next;
--index;
}
myListNode* delNode = p->next;;
p->next = p->next->next;
m_size -= 1;
delete delNode;
}
};
LeetCode | 707. 设计链表的更多相关文章
- Java实现 LeetCode 707 设计链表(环形链表)
707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...
- LeetCode——707 设计链表
题目: 总而言之就是要用C++手撸链表,我的代码: class MyLinkedList { public: /** Initialize your data structure here. */ M ...
- LeetCode 707 ——设计链表
1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...
- LeetCode——142 设计链表2
题目 代码 class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head, *slow ...
- LeetCode——141 设计链表
题目: 简单说下思路: 用两个指针,一个跑得快,一个跑得慢(例如一个每次前进两步,一个前进一步),这样只要快指针不会撞上NULL(如果遇到了NULL的情况那么必然不存在环),快指针肯定会和慢指针碰面( ...
- C#LeetCode刷题-链表
链表篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 19 删除链表的倒数第N个节点 29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- [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 是指向下一个节点的指针/ ...
随机推荐
- UnitTest测试框架-操作步骤
一.UnitTest 1. TestCase 说明:测试用例 1.新建类并集成unittest.TestCase 2. TestSuite 说明:测试套件(多条用例) 方法: 1. 实例化 suite ...
- 吴裕雄--python学习笔记:BeautifulSoup模块
import re import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.co ...
- python基础修改haproxy配置文件
1.通过eval(),可以将字符串转为字典类型. 2.Encode过程,是把python对象转换成json对象的一个过程,常用的两个函数是dumps和dump函数.两个函数的唯一区别就是dump把py ...
- nginx 代理第三方邮件站点
需求:公司业务服务器使用的是阿里云,要求内网(仅有内网IP)所有流量走网关服务器(有外网IP及内网IP),内网服务器需要调用一个公网上的第三方邮件站点.在参考了https://www.linuxba. ...
- 8.2.2 使用Java8增强的Iterator遍历集合元素
8.2.2 使用Java 8增强的Iterator遍历集合元素 Iterator接口方法 程序示例 Iterator仅用于遍历集合 Iterator必须依附于Collection对象 修改迭代变量的值 ...
- 初识Mybatis之工程搭建
简介:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 ...
- 这是100年后火星的未来,到处都是CBD!
火星是个荒漠之地,尽管如此,最近几十年人类一直准备登录火星,然后殖民火星.随着科技的迅猛发展,感觉火星离我们越来越近了.不过,人类如何在火星上生存下去,这一直是科学家们最热衷的话题. 意大利建筑师 ...
- Hexo搭建总结
Hexo搭建过程记录 1.Hexo基本环境搭建 1.Hexo安装前提 Node.js和Git,他们的安装方法可以自行百度. 2.具体安装步骤可以参考: https://www.cnblogs.com/ ...
- array, matrix, list and dataframe
总结一下"入门3R"(Reading, 'Riting, 'Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的. vector 命名 12 month.days ...
- rpmbuild 实践
安装 rpmbuild 1 # yum install -y rpm-build 查看 rpmbuild 相关的宏和参数 12345678 # rpmbuild --showrc | grep --c ...