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++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
随机推荐
- callable和runnable的区别
Runnable接口源码 @FunctionalInterface public interface Runnable { /** * When an object implementing inte ...
- [HNOI2019]JOJO(KMP自动机+主席树)
一道神仙题,考察选手对KMP的深入理解. 先考虑没有2操作的做法.设每一段为一个二元组(x,c),考虑一段前缀匹配后缀,除了第一段的字符,其他段的二元组(x,c)必须相等,所以可以将其视为特殊字符进行 ...
- Regex: positive lookahead 先行断言____ 后行断言(lookbehind)
先行断言: /a(?=b)/ ,positive lookahead,a的后方必须是b才行 /a(?!b)/ ,negative lookahead,a的后方必须不是b才能匹配 如下图示: 来 ...
- 移植linux4.14内核到四核Exynos4412开发板
最近法师收到了很多留言,其中有一部分问法师什么时候更新,还有一大部分问法师我是买迅为的IMX6UL精英版好呢还是买4412精英版好呢,因为我们这俩个都不贵.法师的建议的是入手4412!为什么呢? 第一 ...
- day46-守护线程
#1.守护线程要注意的坑:下面代码只能打印出子线程开始,无法打印出子线程执行完毕,因为主线程在t.start()以后就结束了, #而子线程要睡眠1秒,所以子线程守护线程随着主线程的结束而结束了. fr ...
- 使用命令安装laravel 项目
cp .env.example .env 拷贝.env 文件 php artisan key:generate 生成秘钥 php artisan migrate 生成数据表 composer ...
- [LC] 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- spring和mybatis整合报错:org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyExceptio ...
- leetcode第22题:括号生成
力扣上的题目可以大致分为以下种类: 对某种复杂规则的彻底解析,很有可能要构造状态机,充分考虑边界情况. 对某种数据结构及算法的应用. 对数学概念.遍历.动态规划等的综合应用. 通过分析,本题应该属于1 ...
- MyBatis学习笔记二:MyBatis生产中使用环境搭建
这里是在上一个环境的基础上修改的,这里就不在给出所有的配置,只给出哪里修改的配置 1.修改POJO对象为注解方式 2.创建Dao层接口 package com.orange.dao; import c ...