• 设计链表


    设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 {
        
    public static class ListNode {
        private int val;
        private ListNode next;
    
        public ListNode(int val) {
          this.val = val;
        }
      }
        /** Initialize your data structure here. */
        public MyLinkedList() {
            
        }
        
        private ListNode listNode=null;
        
        /** 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(listNode==null){
                return -1;
            }
            ListNode itr=this.listNode;
            int count=0;
            while(itr!=null){
                if(count==index){
                    return itr.val;
                }
                count++;
                itr=itr.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. */
        public void addAtHead(int val) {
            ListNode newNode=new ListNode(val);
            newNode.next=this.listNode;
            this.listNode=newNode;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            ListNode itr=this.listNode;
            ListNode last=null;
            ListNode newNode=new ListNode(val);
            while(itr!=null){
                if(itr.next==null){
                    last=itr;
                }
                itr=itr.next;
            }
            if(last!=null){
                last.next=newNode;
            }else{
                this.listNode=newNode;
            }
        }
        
        /** 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<=0){
                addAtHead(val);
            }else{
             ListNode itr=this.listNode;
            int count=0;
            ListNode newNode=new ListNode(val);
            while(itr!=null){
                if(count+1==index){
                    ListNode tmp=itr.next;
                    itr.next=newNode;
                    if(tmp!=null){
                        newNode.next=tmp;
                    }
                    break;
                }
                count++;
                itr=itr.next;
            }   
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            ListNode itr=this.listNode;
            int count=0;
            ListNode prev=null;
            while(itr!=null){
                if(count==index){
                    ListNode tmp=itr.next;
                    if(prev!=null){
                     prev.next=tmp;   
                    }else{
                     this.listNode=tmp;
                    }
                    break;
                }
                count++;
                prev=itr;
                itr=itr.next;
            }
        }
    }
    
    /**
     * 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);
     */
    View Code

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/design-linked-list

  • 相关阅读:
    shell tr命令的使用
    linux find prune排除某目录或文件
    在vue中使用axios发送post请求,参数方式
    webpack官网demo起步中遇到的问题
    css中盒子模型与box-sizing属性
    jquery获得 url的变量
    17-js观察者模式
    基于Jquery ui 可复用的酒店 web页面选择入住日期插件
    webkit浏览器下改变滚动条样式
    用户登录时,禁止chrome提示用户保存密码
  • 原文地址:https://www.cnblogs.com/wuyouwei/p/11826761.html
Copyright © 2020-2023  润新知