LeetCode--链表1-单链表
LeetCode--链表1-单链表
单链表模板
- 初始化
- 头部插入
- 尾部插入
- 删除节点
- Index插入
- 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-单链表的更多相关文章
- [C++]线性链表之单链表
[文档整理系列] 线性链表之单链表 /* 问题描述:线性表____链表_____单链表 @date 2017-3-7 */ #include<iostream> using namespa ...
- 数据结构5: 链表(单链表)的基本操作及C语言实现
逻辑结构上一个挨一个的数据,在实际存储时,并没有像顺序表那样也相互紧挨着.恰恰相反,数据随机分布在内存中的各个位置,这种存储结构称为线性表的链式存储. 由于分散存储,为了能够体现出数据元素之间的逻辑关 ...
- [LeetCode系列] 双单链表共同节点搜索问题
找到两个单链表的共同节点. 举例来说, 下面两个链表A和B: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 共同节点为c1. 分析: 共同节点距离A,B的起点 ...
- 【Algorithm | 链表】单链表“环”、“环的起点”、“环的长度”问题
参考资料 • Floyd判圈算法 { 链接 } • 单链表“环”.“环的起点”.环的长度”问题 { 链接 } 链表环的问题 一.判断链表有换 使用两个指针slow和fast.两个指针开始时均在头节点处 ...
- js数据结构之链表(单链表、双向链表、循环链表)
首先,链表有以下特点: 1. 存储空间不固定,可灵活扩充 2.方便多次的插入和删除,效率较高 单链表 单链表是最常用的链表,其对数据的操作均为单项的,向后查找的. /* 链表(基于对象) 此处为单链表 ...
- [算法][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 ...
- 用最简单的方式学Python单链表
Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式 ...
- LeetCode刷题总结-链表
LeetCode刷题总结-链表 一.链表 链表分为单向链表.单向循环链表和双向链表,一下以单向链表为例实现单向链表的节点实现和单链表的基本操作. 单向链表 单向链表也叫单链表,是链表中最简单的 ...
- 用最容易的方式学会单链表(Python实现)
单链表与数组 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列也会有如下缺点: 一个动态数组的长度可能超过实际存储数组元素所需 ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
随机推荐
- OfficeidMsoViewer最新版
点击下载 OfficeidMsoViewer最新版
- Spring 的 IOC 和 AOP 的理解
Spring 的 IOC 和 AOP 的理解: https://www.jianshu.com/p/bf1adc3b75e6 对Spring的核心(AOP和IOC)的理解(大白话) https://w ...
- python,PyAutoGUI,设置鼠标键盘自动操作
三个文件需在同一个文件夹下面,文件夹的位置无要求. 1.第一个文件,trial.py.python代码调用PyAutoGUI操作鼠标键盘,可以通过修改start_time和end_time来确定程序自 ...
- mui折叠面板的使用
折叠面板从二级列表中演化而来,dom结构和二级列表类似,如下: <div class="mui-content"> <div class="mui-ca ...
- nutzboot 项目打包排除或指定配置文件(夹)
springboot 是一样的 我这里就是从springboot哪里拿过来的 (nutzboot2.x已测试可以使用) 排除指定文件 在pom 文件 build 标签内添加 resources < ...
- 895A. Pizza Separation#分披萨问题(前缀和)
题目出处:http://codeforces.com/problemset/problem/895/A 题目大意:对于给出的一些角度的披萨分成两份,取最小角度差 #include<stdio.h ...
- 【ubuntu】乱七八糟
https://blog.csdn.net/totorocyx/article/details/80032556 https://blog.csdn.net/weixin_40662331/artic ...
- VisualStudio使用HALCIN_NET控件
遵循以下步骤在一个应用中添加Halcon/.Net: 一. 定义工具箱 Halcon/.net 不仅提供了一个类库,而且提供了一个控件:HWindowControl,它包含一个显示图像处理结果的Hal ...
- ZooKeeper基本讲解及使用
本文摘录于:http://blog.51cto.com/tchuairen/1859494:https://blog.csdn.net/peace1213/article/details/525714 ...
- deeplearning.ai 卷积神经网络 Week 1 卷积神经网络
1. 传统的边缘检测(比如Sobel)手工设计了3*3的filter(或者叫kernel)的9个权重,在深度学习中,这9个权重都是学习出来的参数,会比手工设计的filter更好,不但可以提取90度.0 ...