zoukankan      html  css  js  c++  java
  • [leetCode]707. 设计链表

    在这里插入图片描述

    原始链表操作无哑节点

    class MyLinkedList {
    
        class Node {
            int val;
            Node next;
            Node (int x) {
                val = x;
            }
        }
    
        private Node first = null;
    
        // 链表长度
        private int N = 0;
    
        /** Initialize your data structure here. */
        public MyLinkedList() {
    
        }
        
        /** 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 >= N) return -1;
            Node cur = first;
            while(index-- > 0) {
                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. */
        public void addAtHead(int val) {
            Node lastNode = first;
            first = new Node(val);
            first.next = lastNode;
            N++;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            if (first == null) addAtHead(val);
            Node cur = first;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = new Node(val);
            N++;
        }
        
        /** 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 > N) return;
            if (index <= 0) {
                addAtHead(val);
                return;
            }
            if (index == N) {
                addAtTail(val);
                return;
            }
            Node prev = first;
            Node cur = first.next;
            while (index-- > 1) {
                cur = cur.next;
                prev = prev.next;
            }
            Node insert = new Node(val);
            prev.next = insert;
            insert.next = cur;
            N++;
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= N) return;
            if (index == 0) {
                first = first.next;
                N--;
                return;
            } 
            Node prev = first;
            Node cur = first.next;
            while (index-- > 1) {
                cur = cur.next;
                prev = prev.next;
            }
            prev.next = cur.next;
            N--;
        }
    }
    
    /**
     * 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);
     */
    

    使用哑节点

    class MyLinkedList {
    
        class Node {
            int val;
            Node next;
            Node (int x) {
                val = x;
            }
        }
    
        private Node dummyHead = null;
    
        // 链表长度
        private int N;
    
        /** Initialize your data structure here. */
        public MyLinkedList() {
            dummyHead = new Node(0);
            N = 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 < 0 || index >= N) return -1;
            Node cur = dummyHead;
            while (index-- > 0) {
                cur = cur.next;
            }
            return cur.next.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) {
            Node newNode = new Node(val);
            newNode.next = dummyHead.next;
            dummyHead.next = newNode;
            N++;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        public void addAtTail(int val) {
            Node cur = dummyHead;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = new Node(val);
            N++;
        }
        
        /** 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 > N) return;
            Node cur = dummyHead;
            while (index-- > 0) {
                cur = cur.next;
            }
            Node newNode = new Node(val);
            newNode.next = cur.next;
            cur.next = newNode;
            N++;
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if (index < 0 || index >= N) return;
            Node cur = dummyHead;
            while (index-- > 0) {
                cur = cur.next;
            }
            cur.next = cur.next.next;
            N--;
        }
    }
    
  • 相关阅读:
    POJ 3278 Catch That Cow
    Object-C 函数定义 -- 笔记
    Object-C变量作用域 -- 笔记
    Object-C 对象 (创建/销毁 对象)-- 笔记
    Object-C 类定义 -- 笔记
    JavaScript 自动分页插件 datatables
    JavaScript 随机数函数
    Server Job: error: String or binary data would be truncated. The statement has been terminated.
    SQL Server error "Xml data type is not supported in distributed queries" and workaround for it
    angularJS怎么实现与服务端的PHP进行数据交互
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13866060.html
Copyright © 2011-2022 走看看