• LeetCode--链表1-单链表


    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);
     */
    
    干啥啥不行,吃饭第一名
  • 相关阅读:
    平衡二叉树 JAVA实现 亲测可用
    八大排序算法 JAVA实现 亲自测试 可用!
    Java 函数传入参数后,究竟发生了什么?java函数传参数原理解析
    运行错误:应用程序无法启动因为并行配置不正确。the application has failed to start because its side-by-side configuration is incorrect 解决方法
    mysql在linux下修改存储路径
    redis订阅与发布系统
    PHP Math常量
    PHP Math函数
    php 字符串函数
    PHP数组函数
  • 原文地址:https://www.cnblogs.com/jiangxinyu1/p/12285004.html
Copyright © 2020-2023  润新知