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. Opencv笔记(十二)——形态学转换

    学习目标: 学习不同的形态学操作,例如腐蚀,膨胀,开运算,闭运算等 我们要学习的函数有: cv2.erode(), cv2.dilate(), cv2.morphologyEx()等 原理简介: 形态 ...

  2. GB35658较796新增检测项部标平台

    GB35658较796新增检测项部标平台总共有113项,总结归类如下:1    报表导出    支持excel格式的报表导出    对查询.统计报表提供excel格式的报表导出    必选:    2 ...

  3. python_4

    1.迭代器:通过iter()方法获得了list的迭代对象,然后就可以通过next()方法来访问list中的元素了,当容器中没有可访问元素时,会抛出StopIteration异常终止迭代器 data = ...

  4. [Algo] 66. All Valid Permutations Of Parentheses I

    Given N pairs of parentheses “()”, return a list with all the valid permutations. Assumptions N > ...

  5. [LC] 270. Closest Binary Search Tree Value

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  6. day37-进程-锁和信号量

    #1.锁:房间的门上有一把锁,锁上有一把钥匙,一个人使用这把钥匙开锁之后,带上钥匙进入房间,把门给反锁了,他在房间干活, # 只要他不出来还锁,别人是无法进入房间的.同时只能有一个人在房间里干活.效率 ...

  7. 跨域问题与SpringBoot解决方案

    什么是跨域? 定义:浏览器从一个域名的网页取请求另一个域名下的东西.通俗点说,浏览器直接从A域访问B域中的资源是不被允许的,如果想要访问,就需要进行一步操作,这操作就叫"跨域".例 ...

  8. mysql操作命令梳理-grant授权和revoke回收权限

    在mysql维护工作中,做好权限管理是一个很重要的环节.下面对mysql权限操作进行梳理: mysql的权限命令是grant,权限撤销的命令时revoke:grant授权格式:grant 权限列表 o ...

  9. iOS燃烧动画、3D视图框架、天气动画、立体相册、微信朋友圈小视频等源码

    iOS精选源码 iOS天气动画,包括太阳,云,雨,雷暴,雪动画. 较为美观的多级展开列表 3D立体相册,可以旋转的立方体 一个仪表盘Demo YGDashboardView 一个基于UIScrollV ...

  10. PMP备考经验总结-1906

    1.考试成绩 3A1T1B 2.考试背景(为什么考) 工作多年,做的项目很多,上到几百万,下到几万的项目,有自己的一套方法论,但是没有系统的对项目的知识做一次完整的梳理整合,觉得在整体把控上需要做一次 ...