LeetCode--链表1-单链表

单链表模板

  1. 初始化
  2. 头部插入
  3. 尾部插入
  4. 删除节点
  5. Index插入
  6. Index返回对应的节点指针和val值
class MyLinkedList {

private:
// 定义单链表的节点
struct ListNode
{
int val;
ListNode* next;
ListNode(int x): val(x) , next(nullptr){}
};
ListNode* head; public:
/** Initialize your data structure here. */
MyLinkedList() : head(nullptr) {} /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if( head == nullptr )
return -1;
if( index <= 0 )
return head->val;
int count = 0 ;
ListNode* p = head;
while( p && count < index )
{
p = p->next;
count ++;
}
if(p)
return p->val;
else
return -1;
} // 在链表头部插入节点
void addAtHead(int val) {
ListNode* node = new ListNode(val);
if( head == nullptr)
{
head = node;
return;
}
node->next = head;
head = node;
} // 在链表尾部插入节点
void addAtTail(int val) {
ListNode* node = new ListNode(val);
// 链表为空 就返回
if(head == nullptr)
{
head == node;
return;
} ListNode* p = head;
while( p->next )
{
p = p->next;
}
p->next = node;
} /** 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* node = new ListNode(val);
if(index <= 0)
addAtHead(val);
int i = 0;
ListNode* p = head;
while(p && i<index - 1)
{
p=p->next;
++i;
}
if(p)
{
node->next = p->next;
p->next = node;
} } /** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if( head==nullptr)
return ;
if( index==0 )
{
ListNode* p = head;
head = head->next;
delete p;
return;
}
ListNode* ps = finder(index-1);
ListNode* p = finder(index);
if( p && ps)
{
ps->next = p->next;
return;
}
else{
ps->next = nullptr;
return ;
} } // 给定下标,返回节点的指针
ListNode* finder (int index)
{
if( head == nullptr)
return nullptr;
if( index <= 0 )
return head;
int count = 0 ;
ListNode* p = head;
while ( p && count < index)
{
p = p->next;
count ++;
}
if(p)
return p;
else
return nullptr;
}
}; /**
* 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);
*/

LeetCode--链表1-单链表的更多相关文章

  1. [C++]线性链表之单链表

    [文档整理系列] 线性链表之单链表 /* 问题描述:线性表____链表_____单链表 @date 2017-3-7 */ #include<iostream> using namespa ...

  2. 数据结构5: 链表(单链表)的基本操作及C语言实现

    逻辑结构上一个挨一个的数据,在实际存储时,并没有像顺序表那样也相互紧挨着.恰恰相反,数据随机分布在内存中的各个位置,这种存储结构称为线性表的链式存储. 由于分散存储,为了能够体现出数据元素之间的逻辑关 ...

  3. [LeetCode系列] 双单链表共同节点搜索问题

    找到两个单链表的共同节点. 举例来说, 下面两个链表A和B: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 共同节点为c1. 分析: 共同节点距离A,B的起点 ...

  4. 【Algorithm | 链表】单链表“环”、“环的起点”、“环的长度”问题

    参考资料 • Floyd判圈算法 { 链接 } • 单链表“环”.“环的起点”.环的长度”问题 { 链接 } 链表环的问题 一.判断链表有换 使用两个指针slow和fast.两个指针开始时均在头节点处 ...

  5. js数据结构之链表(单链表、双向链表、循环链表)

    首先,链表有以下特点: 1. 存储空间不固定,可灵活扩充 2.方便多次的插入和删除,效率较高 单链表 单链表是最常用的链表,其对数据的操作均为单项的,向后查找的. /* 链表(基于对象) 此处为单链表 ...

  6. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  7. 用最简单的方式学Python单链表

    Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...

  8. LeetCode刷题总结-链表

    LeetCode刷题总结-链表 一.链表     链表分为单向链表.单向循环链表和双向链表,一下以单向链表为例实现单向链表的节点实现和单链表的基本操作. 单向链表 单向链表也叫单链表,是链表中最简单的 ...

  9. 用最容易的方式学会单链表(Python实现)

    单链表与数组 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列也会有如下缺点: 一个动态数组的长度可能超过实际存储数组元素所需 ...

  10. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

随机推荐

  1. python基础——认识(if __name__ == ‘__main__’:)

    我们在写代码时,经常会用到这一句:if __name__ == '__main__',那么加这一句有什么用呢?实际上,它起到到了一个代码保护功能,它能够让别人在导入你写的模块情况下,无法看到和运行if ...

  2. springCloud负载均衡Ribbon和Feign的区别

    1.什么是负载均衡: 负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求/数据[均匀]分摊到多个操作单元上执行,负载均衡的关键在于[均匀]. 2.常见的负 ...

  3. [LC] 809. Expressive Words

    Example: Input: S = "heeellooo" words = ["hello", "hi", "helo&quo ...

  4. LeetCode No.115,116,117

    No.115 NumDistinct 不同的子序列 题目 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且 ...

  5. 《ECMAScript 6 入门教程 - 阮一峰著》学习笔记

    在刷LeetCode的过程中看到很多新的语法糖,系统学习一下以便代码更加规范,美观,健壮.

  6. java高并发之线程池

    Java高并发之线程池详解   线程池优势 在业务场景中, 如果一个对象创建销毁开销比较大, 那么此时建议池化对象进行管理. 例如线程, jdbc连接等等, 在高并发场景中, 如果可以复用之前销毁的对 ...

  7. VBA编程常用词汇英汉对照表

    表 20‑1到表 20‑8是VBA编程中使用频率最高的英文单词,按字母排序.词性列中,a表示形容词,n表示名词,v表示动词,p表示介词以及其他词性. 表 20‑1 VBA编程常用词汇表 单词 中文 词 ...

  8. iOS 仿看了吗应用、指南针测网速等常用工具、自定义弹出视图框架、图片裁剪、内容扩展等源码

    iOS精选源码 扩展内容的cell - folding-cell 一个近乎完整的可识别中国身份证信息的Demo 可自动快速... JPImageresizerView 仿微信的图片裁剪 带年月和至今以 ...

  9. Qt QLabel show 显示图像、填充、缩放

    主要成员函数: 1.void setText(QString); //设置label框内的文本. 2.void hide(); //隐藏label框. 3.void setBuddy(QWidget* ...

  10. [Python] 使用Python 3 下载麦子学院视频

    本文基于Python 3,下载麦子学院的视频课程. 本项目只是针对某个具体课程的链接,去寻找该课程所有课时的视频链接并进行下载. 整个项目是非常简单的. 主要涉及的Python: 网络相关:reque ...