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++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
随机推荐
- Codeforces Round #556(Div.1)
A 容易发现i,i+1至少有一个数出现,于是可以让尽量多的2和奇数出现 #include<bits/stdc++.h> using namespace std; int n,s1,s2; ...
- servletHomeWork
2. http全称是什么? 超文本传输协议(HTTP, HyperText Transfer Protocol)是互联网上应用为最广泛的一种网络协议. 3.http协议是无状态的协议是什么意思?请说明 ...
- 21)PHP,杨辉三角
代码展示: $n=; ;$i<=$n;$i++){ ;$k<=$i;$k++){ ||$k==$i){ $arr[$i][$k]=; }else{ ){ $arr[$i][$k] = $a ...
- Serverless 基本概念入门
从行业趋势看,Serverless 是云计算必经的一场革命 2019 年,Serverless 被 Gartner 称为最有潜力的云计算技术发展方向,并被赋予是必然性的发展趋势.Serverless ...
- Tukey法
Tukey法 在介绍Tukey方法前,首先了解学生化极差分布. 在概率论和统计学中,学生化极差分布是极差的抽样分布.该分布是一种连续型概率分布,用于在样本量较小且总体标准差未知的情况下估计正态分布总体 ...
- git 首次提交
git init# 将本地仓库与码云远程仓库进行关联 git remote add origin git的url地址 git add . git commit -m "描述" # ...
- 常见的Java不规范代码
1.格式化源代码 Ctrl + Shift + F – 格式化源代码. Ctrl + Shift + O – 管理import语句并移除未使用的语句 除了手动执行这两个功能外,你还可以让Eclipse ...
- getRandomInt getRandomString
import java.util.concurrent.ThreadLocalRandom; private static final String AB = "ABCDEFGHIJKLMN ...
- Oracle中实现sql查询得到连续号码段
一.表名为t的表中数据如下: select * from t; FPHM KSHM ---------- ---------- 实现代码如下: select b.fphm,min(b.kshm),ma ...
- 爬虫之使用requests爬取某条标签并生成词云
一.爬虫前准备 1.工具:pychram(python3.7) 2.库:random,requests,fake-useragent,json,re,bs4,matplotlib,worldcloud ...