zoukankan      html  css  js  c++  java
  • LeetCode:设计链表

    设计链表

    要求

    链表增删改功能实现

    示例

    struct listnode{
        int val;
        listnode *nextNode;
        listnode(int x):val(x),nextNode(NULL){}
    };
    class MyLinkedList {
        listnode *head;
        int size;
    public:
        /** Initialize your data structure here. */
        MyLinkedList() {
            head=new listnode(-1);
            size=0;                        
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        int get(int index) {
            if(index>=size||index<0)return -1;
            listnode *p=head;
            for (int i = 0; i <=index; ++i)
            {
                p=p->nextNode;
            }
            return p->val;
        }
        
        /** 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) {
            addAtIndex(0,val);
        }
        
        /** Append a node of value val to the last element of the linked list. */
        void addAtTail(int val) {
            addAtIndex(size,val);
        }
        
        /** 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 *p=head;
            for(int i=0;i<index;++i){
                p=p->nextNode;
            }
            listnode *NewNode=new listnode(val);
            if (index<size)
            {
                NewNode->nextNode=p->nextNode;
                p->nextNode=NewNode;
            }
            else
            {
                p->nextNode=NewNode;
            }
            ++size;
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        void deleteAtIndex(int index) {
            if (index>=0&&index<size)
            {
                listnode *p=head,*p1;
                for (int i = 0; i <index; ++i)
                {
                    p=p->nextNode;
                }
                p1=p->nextNode;
                p->nextNode=p1->nextNode;
                delete p1;
                --size;
            }
        }
    };
    
    /**
     * 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);
     */
    

    题目链接

    <>

    持之以恒的学习,方是进步的唯一途径!
  • 相关阅读:
    Hibernate,get()和load()区别
    Hibernate,Session方法使得java对象进入持久化状态;持久化对象特征
    Hibernate,Session清理缓存时间点
    frameset子窗口获取父窗口失败原因?
    struts2,实现Ajax异步通信
    struts2-json-plugin插件实现异步通信
    Hibernate,JPA注解@ManyToMany_JoinTable
    Hibernate,JPA注解@ManyToMany
    SparkStreaming操作Kafka
    DirectStream、Stream的区别-SparkStreaming源码分析02
  • 原文地址:https://www.cnblogs.com/breezy-ye/p/12753858.html
Copyright © 2011-2022 走看看