zoukankan      html  css  js  c++  java
  • [Leetcode]设计链表

    题目

    设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 nextval 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

    在链表类中实现这些功能:

    • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1
    • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
    • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。
    • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

    示例:

    MyLinkedList linkedList = new MyLinkedList();
    linkedList.addAtHead(1);
    linkedList.addAtTail(3);
    linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
    linkedList.get(1);            //返回2
    linkedList.deleteAtIndex(1);  //现在链表是1-> 3
    linkedList.get(1);            //返回3
    

    提示:

    • 所有值都在 [1, 1000] 之内。
    • 操作次数将在  [1, 1000] 之内。
    • 请不要使用内置的 LinkedList 库

     代码

    typedef struct DoubleList{
        int val;
        DoubleList* pre;
        DoubleList* next;
        DoubleList(int value):val(value),pre(nullptr),next(nullptr){}
    };
    class MyLinkedList {
    public:
        /** Initialize your data structure here. */
        MyLinkedList(): _head(new DoubleList(0)) {
           
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        int get(int index) {
            auto ptr=_head;
            while(ptr->next!=nullptr)
            {
                if(index==0)
                {
                    return ptr->next->val;
                }
                index--;
                ptr=ptr->next;
            }
            return -1;
        }
        
        /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
        void addAtHead(int val) {
            auto ptr=new DoubleList(val);
            ptr->next=_head->next;
            if(_head->next!=nullptr)
             _head->next->pre=ptr;
            _head->next=ptr;
            ptr->pre=_head;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        void addAtTail(int val) {
            auto ptr=_head;
            while(ptr->next!=nullptr)
                ptr=ptr->next;
            ptr->next=new DoubleList(val);
            ptr->next->pre=ptr;
        }
        
        /** 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) {
            auto ptr=_head;
            while(ptr->next!=nullptr)
            {
                if(index==0)
                {
                    auto newNode=new DoubleList(val);
                    newNode->next=ptr->next;
                    ptr->next=newNode;
                    newNode->pre=ptr;
                    if(newNode->next!=nullptr)
                    {
                        newNode->next->pre=newNode;
                    }
                    return ;
                }
                index--;
                ptr=ptr->next;
            }
            if(index==0)
            {
                auto newNode=new DoubleList(val);
                newNode->pre=ptr;
                ptr->next=newNode;
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        void deleteAtIndex(int index) {
              auto ptr=_head;
            while(ptr->next!=nullptr)
            {
                if(index==0)
                {
                   auto del=ptr->next;
                   ptr->next=del->next;
                   if(del->next!=nullptr)
                   {
                       del->next->pre=ptr;
                   }
                    delete del;
                    return;
                }
                index--;
                ptr=ptr->next;
            }
        }
    private:
        DoubleList* const _head;    
    };
    
    /**
     * 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);
     */
  • 相关阅读:
    .a包生成64位
    iOS教程
    iOS 难题解决日志------2层控制器 上面的控制器显示透明
    企业级的App发布流程
    如何从oc中去获取一个私有的变量.....
    iOS app的破解原理,就是去除已付费的账户信息的原理是什么?
    Could not launch "app_name"
    GCD时间轴
    Win8自动更新怎么关闭 取消Win8自动更新
    python3 elf文件解析
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053563.html
Copyright © 2011-2022 走看看