707. Design Linked List

Question

Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1. addAtHead(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. addAtTail(val) : Append a node of value val to the last element of the linked list. addAtIndex(index, 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. deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2);  // linked list becomes 1->2->3
linkedList.get(1);            // returns 2
linkedList.deleteAtIndex(1);  // now the linked list is 1->3
linkedList.get(1);            // returns 3

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in LinkedList library.

Solution

  • Method 1:
    class MyLinkedList {
        private static class Node{
            int val;
            Node next;
            public Node(int val){
                this.val = val;
            }
        }
        private Node dummy;
        private int size;
        private Node tail;
        /** Initialize your data structure here. */
        public MyLinkedList() {
            this.dummy = new Node(0);
            this.size = 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 || index < 0) return -1;
            int count = 0;
            Node cur = dummy.next;
            while(count < index){
                cur = cur.next;
                count++;
            }
            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 node = new Node(val);
            Node originalHead = dummy.next;
            size++;
            dummy.next = node;
            if(originalHead == null){
                this.tail = node;
                return;
            }
            node.next = originalHead;
        }
    
        /** 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(size++ == 0){
                dummy.next = node;
                this.tail = node;
            }else{
                tail.next = node;
                tail = tail.next;
            }
        }
    
        /** 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){
                addAtTail(val);
            }else if(index < size){
                Node pre = dummy;
                int count = 0;
                Node node = new Node(val);
                while(count < index){
                    count++;
                    pre = pre.next;
                }
                node.next = pre.next;
                pre.next = node;
                size++;
            }else if(index < 0) addAtHead(val);
        }
    
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if(index == size - 1){
                Node pre = dummy, cur = dummy.next;
                while(cur.next != null){
                    cur = cur.next;
                    pre = pre.next;
                }
                this.tail = pre;
                tail.next = null;
                size--;
            }else if(index < size - 1 && index >= 0){
                int count = 0;
                Node cur = dummy;
                while(count < index){
                    count++;
                    cur = cur.next;
                }
                cur.next = cur.next.next;
                size--;
            }
        }
    }
    
    /**
     * 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);
     */
    
  • Method 2: Bi-directional linked list
    class MyLinkedList {
        private static class Node{
            public int val;
            public Node pre, next;
            public Node(int val){
                this.val = val;
            }
        }
        /** Initialize your data structure here. */
        private Node dummy;
        private Node tail;
        private int size;
        public MyLinkedList() {
            this.dummy = new Node(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 && index >= 0){
                int count = 0;
                Node cur = dummy;
                while(count < index){
                    cur = cur.next;
                    count++;
                }
                return cur.next.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 originalHead = dummy.next;
            Node node = new Node(val);
            node.pre = dummy;
            dummy.next = node;
            node.next = originalHead;
            if(originalHead != null) originalHead.pre = node;
            if(tail == null) tail = node;
            size++;
        }
    
        /** 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(tail == null){
                this.tail = node;
                dummy.next = node;
                node.pre = dummy;
            }else{
                this.tail.next = node;
                node.pre = tail;
                tail = node;
            }
            size++;
        }
    
        /** 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){
                addAtTail(val);
            }else if(index < 0){
                addAtHead(val);
            }else if(index < size){
                int count = 0;
                Node cur = dummy;
                while(count < index){
                    cur = cur.next;
                    count++;
                }
                Node node = new Node(val);
                Node next = cur.next;
                cur.next = node;
                node.pre = cur;
                node.next = next;
                next.pre = node;
                size++;
            }
        }
    
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void deleteAtIndex(int index) {
            if(index >= size || index < 0) return;
            if(index == size - 1){ // delete the tail
                if(tail.pre == dummy){
                    tail = null;
                    dummy.next = null;
                }else{
                    tail = tail.pre;
                    tail.next = null;
                }
            }else{
                int count = 0;
                Node cur = dummy;
                while(count < index){
                    count++;
                    cur = cur.next;
                }
                cur.next = cur.next.next;
                cur.next.pre = cur;
            }
            size--;
        }
    }
    
    /**
     * 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);
     */