• 力扣707题(设计链表)


    707、设计链表

    具体实现:

    1.使用单向链表

    删除链表节点:

    添加链表节点:

    2.使用双向链表

    删除链表节点:

     

    添加链表节点:

     

    代码:

    单向链表

    class ListNode{//单向链表
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int val){
            this.val = val;
        }
    }
    class MyLinkedList {
        int size;//存储链表元素的个数
        ListNode head;//虚拟头结点
        public MyLinkedList() {//初始化链表
            size = 0;
            head = new ListNode(0);
        }
        
        public int get(int index) {//获取第index个节点的数值
            if (index < 0 || index >= size){
                return -1;
            }
            ListNode currentNode = head;
            for (int i = 0; i <= index; i++){
                currentNode = currentNode.next;
            }
            return currentNode.val;
        }
        
        public void addAtHead(int val) {//在链表最前面插入一个节点
            addAtIndex(0, val);
        }
        
        public void addAtTail(int val) {//在链表的最后插入节点
            addAtIndex(size, val);
        }
        
        public void addAtIndex(int index, int val) {
            if (index > size){
                return;
            }
            if (index < 0){
                index = 0;
            }
            size++;//插入一个节点链表变长
    
            ListNode pred = head;
            for (int i = 0; i < index; i++){
                pred = pred.next;//找到要插入节点的前驱
            }
            ListNode toAdd = new ListNode(val);
            toAdd.next = pred.next;
            pred.next = toAdd;
        }
        
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size){
                return;
            }
            size--;
            ListNode pred = head;
            for (int i = 0; i < index; i++){
                pred = pred.next;
            }
            pred.next = pred.next.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);
     */

    2.双向链表

    class ListNode{//双向链表
        int val;
        ListNode next, prev;
        ListNode(int x){val = x;}
        
    }
    class MyLinkedList {
        int size;//存储链表元素的个数
        ListNode head;//虚拟头结点
        ListNode tail;
        public MyLinkedList() {//初始化链表
            size = 0;
            head = new ListNode(0);
            tail = new ListNode(0);
            head.next = tail;
            tail.prev = head;
        }
        
        public int get(int index) {//获取第index个节点的数值
            if (index < 0 || index >= size){
                return -1;
            }
            ListNode currentNode = head;
            if (index < (size -1)/2){  // 通过判断 index < (size - 1) / 2 来决定是从头结点还是尾节点遍历,提高效率
                for (int i = 0; i <= index; i++){
                    currentNode = currentNode.next;
                }
            }else {
                currentNode = tail;
                for (int i = 0; i <= size - index - 1; i++){
                    currentNode = currentNode.prev;
                }
            }
            
            return currentNode.val;
        }
        
        public void addAtHead(int val) {//在链表最前面插入一个节点
            addAtIndex(0, val);
        }
        
        public void addAtTail(int val) {//在链表的最后插入节点
            addAtIndex(size, val);
        }
        
        public void addAtIndex(int index, int val) {
            if (index > size){
                return;
            }
            if (index < 0){
                index = 0;
            }
            size++;//插入一个节点链表变长
    
            ListNode pred = head;
            for (int i = 0; i < index; i++){
                pred = pred.next;//找到要插入节点的前驱
            }
            ListNode toAdd = new ListNode(val);
            toAdd.next = pred.next;
            pred.next.prev = toAdd;
            toAdd.prev = pred;
            pred.next = toAdd;
        }
        
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= size){
                return;
            }
            size--;
            ListNode pred = head;
            for (int i = 0; i < index; i++){
                pred = pred.next;
            }
            pred.next.next.prev = pred;
            pred.next = pred.next.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);
     */
  • 相关阅读:
    20201015-3 每周例行报告
    20201008-1 每周例行报告
    20200924-1 每周例行报告
    20200924-3 单元测试,结对
    刷题-Leetcode-120. 三角形最小路径和
    刷题-Leetcode-1025. 除数博弈
    刷题-Leetcode-217. 存在重复元素
    刷题-Leetcode-24.两两交换链表中的节点
    刷题-AcWing-104. 货仓选址
    ARP报文抓包解析实验报告
  • 原文地址:https://www.cnblogs.com/zhaojiayu/p/15390058.html
Copyright © 2020-2023  润新知