707. Design Linked List

707. Design Linked List

class MyLinkedList {
    
    class Node {
        int val;
        Node next;
        public Node(int val) {
            this.val = val;
        }
    }

    int length; 
    Node head;
    Node tail; // 这个可以省略
    
    /** Initialize your data structure here. */
    public MyLinkedList() {
        length = 0;
        head = null;
        tail = 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 (index >= 0 && index < length) { //index is valid
            Node node = head;
            for (int i = 0; i < index; i++) node = node.next;
            return node.val;
        }
        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) {
        Node node = new Node(val);
        if (length == 0) {
            head = node;
            tail = node; // 可以省略
        } else {
            node.next = head;
            head = node;
        }
        length++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node node = new Node(val);
        if (length == 0) {
            head = node;
            tail = node; // 可以省略
        } else {
            // 这是有tail的情况
            tail.next = node;
            tail = node;
            // 没有tail要这样写
            Node curr = head;
            while (curr.next != null) curr = curr.next;
            curr.next = node;
            tail = node
        }
        length++;
    }
    
    /** 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 if (index == length) addAtTail(val);
        else if (index > 0 && index < length) {
            Node node = new Node(val);
            Node curr = head;
            // note here i + 1 < index because we need to get the node before the index
            for(int i = 0; i + 1 < index; i++) curr = curr.next;
            node.next = curr.next;
            curr.next = node;
            length++;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if (index < 0 || index >= length) return;
        
        if (index == 0) {
            if (length == 1) {
                head = null;
                tail = null; // 可以省略
            } else {
                head = head.next;
            }
        } else {
            Node curr = head;
            // note here i + 1 < index because we need to get the node before the index
            for(int i = 0; i + 1 < index; i++) curr = curr.next;
            Node nodeToDelete = curr.next;
            if (nodeToDelete.next == null) curr.next = null;
            else curr.next = curr.next.next;
            if (index == length - 1) tail = curr; // 可以省略
        }  
        length--;
    }
}

Last updated