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++实现(采用模板类)
		
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
 
随机推荐
- XEN 3166
			
XEN 3166 这题原题是spj,校oj上只用判断yes no,不过也差不多 题意分析之后就是求两个东西: 字典序最小的长度为m的子序列 同时这个字典序严格大于某个字符串 用序列自动机 先尽量相同, ...
 - facebook第三方登陆(使用sharedSDK)无法加载网址:这个URL的域名未包含应用的域名
			
http://bbs.mob.com/forum.php?mod=viewthread&tid=8134&extra=page%3D1
 - day09-正侧表达式
			
while True: phone_num = input('please input your phone_num:') if len(phone_num) == 11 \ and phone_nu ...
 - windows下使用apache相关资料汇总
			
1.Apache httpd.conf配置详解 https://www.cnblogs.com/langren1992/p/5160912.html 2.windows下使用apache经验总结 ht ...
 - 牛客-Forsaken的数列(Treap)
			
题目传送门 sol:第一次看题还真信了是用线段树来做,但是没什么想法,看了题解发现是我不会的Treap,然后花了几天时间学习了一下并补掉题目 无旋Treap #include <bits/std ...
 - Office 365管理员添加自定义域名
			
添加自定义域,以便Office 365允许更短.更熟悉的的电子邮件或用户ID用于服务 一.Office 365小型企业版添加自定义域名 1.使用Office 365管理员账户登陆到由世纪互联运营的Of ...
 - [LC] 13. Roman to Integer
			
Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value I 1 V ...
 - 3)ARP到底属于网络层还是链路层
			
说白了 就是有些协议起到了承上启下的作用 比较模糊 很难给出一个精确的定位
 - 75)PHP,session在使用时的一些语法问题
			
(1)cookie仅能存字符串类型,但是session能存任何数据类型,比如: 然后我在session_2.php中输出这个session_1.php的数据: 结果展示: 我得在浏览器的地址栏中先请求 ...
 - makefile中的变量赋值
			
在makefile中赋值方式有:'='.':='.'?='和'+='. A = a $(B) B = b all: echo $(A) #运行结果:echo a b a b 这种赋值方式是没有先后顺序 ...