zoukankan      html  css  js  c++  java
  • LeetCode Notes_#707_设计链表

    LeetCode Notes_#707_设计链表

    Contents

    题目

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

    • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
    • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
    • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点* 将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    • 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

    提示:

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

    解答

    方法1:单链表

    传统的单链表方法,实现起来较为简单,但是效率略差。

    class MyLinkedList {
        private class ListNode{
            int val;
            ListNode next;
            ListNode(int aVal) {val = aVal;}
        }
    
        int size;
        ListNode dummyHead;
    
        /** Initialize your data structure here. */
        public MyLinkedList() {
            size = 0;
            dummyHead = new ListNode(0);
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        public int get(int index) {
            if(index > size - 1 || index < 0) return -1;
            ListNode cur = dummyHead.next;
            int count = 0;
            while(cur != null){
                if(count == index) return cur.val;
                cur = cur.next;
                count++;
            }
            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. */
        public void addAtHead(int val) {
            addAtIndex(0, val);
        }
        
        /** Append a node of value val to the last element of the linked list. */
        public 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. */
        public void addAtIndex(int index, int val) {
            //index超出size,那么什么也不做,返回
            if(index > size) return;
            else {
                int step = index < 0? 0:index;
                ListNode cur = dummyHead;
                //获得index-1处的pre节点,保存下来
                for(int i = 1;i <= index ;i++){
                    cur = cur.next;
                }
                ListNode pre = cur;
                ListNode indexNode = cur.next;
                ListNode newNode = new ListNode(val);
                newNode.next = indexNode;
                pre.next = newNode;
                size++;
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            //索引无效,那么什么也不做
            if(index > size - 1 || index < 0) return;
            else {
                ListNode cur = dummyHead;
                for(int i = 1;i <= index;i++){
                    cur = cur.next;
                }
                ListNode pre = cur;
                ListNode next = cur.next.next;
                pre.next = next;
                size--;
            }
        }
    }

    复杂度分析

    时间复杂度:
    addAtHead()O(1)
    addAtTail()O(n)
    get(),addAtIndex(),deleteAtIndex()O(index)
    空间复杂度:
    所有函数的空间复杂度是O(1)

    方法2:双链表

    双链表指的是每个节点有两个指针prevnext。不仅可以向后查找,还可以向前查找。那么在查找索引为index的节点时,最多只需要遍历半个链表,提高了操作效率。

    class MyLinkedList {
        private class ListNode{
            int val;
            ListNode prev;
            ListNode next;
            ListNode(int aVal){val = aVal;}
        }
    
        int size;
        ListNode dummyHead,dummyTail;
    
        /** Initialize your data structure here. */
        public MyLinkedList() {
            size = 0;
            dummyHead = new ListNode(0);
            dummyTail = new ListNode(0);
            dummyHead.next = dummyTail;
            dummyTail.next = dummyHead;
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        public int get(int index) {
            if(index < 0 || index > size - 1) return -1;
            //从dummyHead到dummyTail总路程是size + 1,比较从头走更快还是从尾走更快
            //从头走,需要走index + 1步;从尾走,需要走size - index步。
            ListNode cur = dummyHead;
            if(index + 1 < size - index){
                for(int i = 1;i <= index + 1;i++){
                    cur = cur.next;
                }
            }
            else{
                cur = dummyTail;
                for(int i = 1;i <= size - index;i++){
                    cur = cur.prev;
                }
            }
            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. */
        public void addAtHead(int val) {
            addAtIndex(0, val);
        }
        
        /** Append a node of value val to the last element of the linked list. */
        public 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. */
        public void addAtIndex(int index, int val) {
            if(index > size) return;
            else{
                ListNode newNode = new ListNode(val);
                ListNode cur = dummyHead;
                if(index < 0) index = 0;
                //走到index节点的前一个节点位置
                if(index < size + 1 - index){
                    for(int i = 1;i <= index;i++){
                        cur = cur.next;
                    }
                }
                else{
                    cur = dummyTail;
                    for(int i = 1;i <= size - index + 1;i++){
                        cur = cur.prev;
                    }
                }
                newNode.prev = cur;
                newNode.next = cur.next;
                //对于index = size的情况,cur.next是null,所以要加入条件判断
                if(cur.next != null) cur.next.prev = newNode;
                cur.next = newNode;
                size++;
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if(index < 0 || index > size - 1) return;
            else{
                ListNode cur = dummyHead;
                if(index < size + 1 - index){
                    for(int i = 1;i <= index;i++){
                        cur = cur.next;
                    }
                }
                else{
                    cur = dummyTail;
                    for(int i = 1;i <= size - index + 1;i++){
                        cur = cur.prev;
                    }
                }
                cur.next = cur.next.next;
                //针对index = size的情况,增加条件判断
                if(cur.next != null) cur.next.prev = cur;
                size--;
            }
        }
    }

    复杂度分析

    时间复杂度:
    addAtHead()O(1)
    addAtTail()O(1)
    get(),addAtIndex(),deleteAtIndex()O(min(index,n-index))<=O(n/2)
    空间复杂度:
    所有函数的空间复杂度是O(1)

  • 相关阅读:
    页面渲染速度增加的方法和建议
    五(六)、Package & import
    五(五)、构造器 & JavaBean &this
    五(四)、封装性
    五(三)、方法
    五(二)、匿名对象
    五(一)、类&对象概述
    六、java 异常处理
    四、java 数组
    三、java 基础提炼
  • 原文地址:https://www.cnblogs.com/Howfars/p/13542075.html
Copyright © 2011-2022 走看看