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

    题目:

    总而言之就是要用C++手撸链表,我的代码:

    class MyLinkedList {
    public:
        /** Initialize your data structure here. */
        MyLinkedList() {
            head = NULL;
            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<0 || index >= size){
                return -1;
            }
            Node *cur = head;
            for(int i=0; i<index; i++){
                cur = cur->next;
            }
            return cur->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) {
            Node *newhead = new Node(val, head);
            head = newhead;
            size++;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        void addAtTail(int val) {
            Node *tail = new Node(val, NULL);
            Node *cur = head;
            while(cur->next)cur = cur->next;
            cur->next = tail;
            size++;
        }
        
        /** 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) {
            if(index>size){
                return;
            }
            if(index==0||index==-1){
                addAtHead(val);
                return;
            }
            if(index==size){
                addAtTail(val);
                return;
            }
            Node *cur = head;
            for(int i=0; i<index-1; i++){
                cur = cur->next;
            }
            Node *toAdd = new Node(val, cur->next);
            cur->next = toAdd;
            size++;
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        void deleteAtIndex(int index) {
            if(index<0 || index >= size) return;
            Node *cur = head;
            if(index == 0){
                head = head->next;
                size--;
                delete cur;
                return;
            }
            for(int i=0; i<index-1; i++){
                cur = cur->next;
            }
            Node *toFree = cur->next;
            cur->next = cur->next->next;
            delete toFree;
            size--;
        }
    
    private:
        struct Node{
            int val;
            Node *next;
            Node(int x, Node *n): val(x), next(n) {};
        };
        Node *head;
        int size;
    };
    
    

    这题其实很简单,这里的实现方式是单链表,基本上就是数据结构的知识点,所以没什么好提的。但是万恶的LeetCode有这么个测试样例:

    也就是说,当输入的index-1时要当成0来处理,但是这一点题目里面完全没有提及。只需要改一下判定条件就可以了(把void addAtIndex(int index, int val)里的if(index==0)改成if(index==0||index==-1)),除了这个问题之外基本上没什么要注意的了。

    本博客文章默认使用CC BY-SA 3.0协议。
  • 相关阅读:
    Asp.net Treeview 客户端选中效果实现 (初级)
    MYSQL生成日历表,通常在做报表的时候需要用来生成一个临时表,用来左连接等。
    写了一个抽奖类,感觉还不错,可以适合各种变化
    将系统的内部类:HttpValueCollection 移到自己的系统中,使其能方便的解析id=1&name=张三&sex=男这样的字符串参数 querystring
    指定某个文件的创建 修改 访问时间
    Reqeust["keyname"] 的读取顺序
    pku1463 Strategic game
    pku1947 Rebuilding Roads
    pku1848 Tree
    pku1056 IMMEDIATE DECODABILITY
  • 原文地址:https://www.cnblogs.com/yejianying/p/leetcode_707.html
Copyright © 2011-2022 走看看