Design your implementation of the linked list. You can choose to use a singly or 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 the MyLinkedList class:


	MyLinkedList() Initializes the MyLinkedList object.
	int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
	void addAtHead(int 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.
	void addAtTail(int val) Append a node of value val as the last element of the linked list.
	void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
	void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.


&nbsp;
Example 1:

Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]

Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2);    // linked list becomes 1-&gt;2-&gt;3
myLinkedList.get(1);              // return 2
myLinkedList.deleteAtIndex(1);    // now the linked list is 1-&gt;3
myLinkedList.get(1);              // return 3


&nbsp;
Constraints:


	0 &lt;= index, val &lt;= 1000
	Please do not use the built-in LinkedList library.
	At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.


class Node:
    def __init__(self, val: int):
        self.val = val
        self.next = None
        self.prev = None
        
class MyLinkedList:
    def __init__(self):
        self.head = Node(0)
        self.tail = Node(0)
        self.head.next = self.tail
        self.tail.prev = self.head
        self.size = 0
        
    def get(self, index: int) -> int:
        if index < 0 or index >= self.size:
            return -1
        # Distance of index is closer to head
        if index + 1 < self.size - index:
            curr = self.head
            for i in range(index + 1):
                curr = curr.next
        # Distance of index is closer to tail
        else:
            curr = self.tail
            for i in range(self.size - index):
                curr = curr.prev
        return curr.val

    def addAtHead(self, val: int) -> None:
        curr = Node(val)
        prevNode = self.head
        nextNode = self.head.next
        self.size += 1
        curr.prev = prevNode
        curr.next = nextNode
        prevNode.next = curr
        nextNode.prev = curr

    def addAtTail(self, val: int) -> None:
        curr = Node(val)
        prevNode = self.tail.prev
        nextNode = self.tail
        self.size += 1
        curr.prev = prevNode
        curr.next = nextNode
        prevNode.next = curr
        nextNode.prev = curr

    def addAtIndex(self, index: int, val: int) -> None:
        curr = Node(val)
        if index > self.size:
            return
        if index < 0:
            index = 0
        if index < self.size - index:
            prevNode = self.head
            for i in range(index):
                prevNode = prevNode.next
            nextNode = prevNode.next
        else:
            nextNode = self.tail
            for i in range(self.size - index):
                nextNode = nextNode.prev
            prevNode = nextNode.prev
        self.size += 1
        curr.prev = prevNode
        curr.next = nextNode
        prevNode.next = curr
        nextNode.prev = curr
        
    def deleteAtIndex(self, index: int) -> None:
        if index < 0 or index >= self.size:
            return 
        if index < self.size - index:
            prevNode = self.head
            for i in range(index):
                prevNode = prevNode.next
            nextNode = prevNode.next.next
        else:
            nextNode = self.tail
            for i in range(self.size - index - 1):
                nextNode = nextNode.prev
            prevNode = nextNode.prev.prev
        self.size -= 1
        prevNode.next = nextNode
        nextNode.prev = prevNode


# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
class MyLinkedList {

    public class ListNode {
        public int val;
        public ListNode next;

        public ListNode() {

        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    private ListNode head;
    private ListNode tail;
    private int length;

    public MyLinkedList() {
        length = 0;
    }

    public int get(int index) {
        if (index > length - 1 || index < 0) return -1;
        int thisIndex = 0;
        ListNode temp = head;
        while (thisIndex != index) {
            temp = temp.next;
            thisIndex++;
        }
        return temp.val;
    }

    public void addAtHead(int val) {
        head = new ListNode(val, head);
        length++;
        if (length == 1) tail = head;
    }

    public void addAtTail(int val) {
        length++;
        if (length == 1) {
            ListNode onlyNode = new ListNode(val);
            head = onlyNode;
            tail = head;
            return;
        }
        tail.next = new ListNode(val);
        tail = tail.next;
    }

    public void addAtIndex(int index, int val) {
        if (index <= length) {
            if (index == 0) {
                addAtHead(val);
                return;
            }
            if (index == length) {
                addAtTail(val);
                return;
            }
            length++;
            ListNode temp = head;
            int thisIndex = 0;
            while (thisIndex != index - 1) {
                temp = temp.next;
                thisIndex++;
            }
            temp.next = new ListNode(val, temp.next);
        }
    }

    public void deleteAtIndex(int index) {
        if (index >= length || index < 0) return;
        length--;
        if (index == 0) {
            head = head.next;
            return;
        }
        ListNode temp = head;
        int thisIndex = 0;
        while (thisIndex != index - 1) {
            temp = temp.next;
            thisIndex++;
        }
        if (index == length) {
            tail = temp;
            temp.next = null;
        }
        else temp.next = temp.next.next;
    }
}
class MyLinkedList {
    struct Node{
        int val;
        Node *next,*prev;
        Node(int x){val=x,next=prev=NULL;}
    };
    Node *head;int size;
public:
    MyLinkedList() {
        head=NULL;size=0;
    }
    
    int get(int index) {
        if(index>=size or index<0) return -1;
        Node *ptr=head;
        for(int i=0;i<index;i++) ptr=ptr->next;
        return ptr->val;
    }
    
    void addAtHead(int val) {
        Node *newNode=new Node(val);
        newNode->next=head;
        if(head) head->prev=newNode;
        head=newNode;
        size++;
    }
    
    void addAtTail(int val) {
        addAtIndex(size,val);
    }
    
    void addAtIndex(int index, int val) {
        if(index<0 or index>size) return;
        if(index==0){
            addAtHead(val);return;
        }
        Node *newNode=new Node(val);
        Node *prev=head;
        for(int i=0;i<index-1;i++){
            prev=prev->next;
        }
        newNode->next=prev->next;
        prev->next=newNode;
        newNode->prev=prev;
        if(newNode->next) newNode->next->prev=newNode;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if(index>=size or index<0) return;
        if(index==0){
            Node *temp=head;
            head=head->next;
            delete temp;size--;
            return;
        }
        Node *del=head;
        for(int i=0;i<index;i++){
            del=del->next;
        }
        if(del->prev) del->prev->next=del->next;
        if(del->next) del->next->prev=del->prev;
        delete del;
        size--;
    }
};
var MyLinkedList = function() {
    this.linked = [];
};

MyLinkedList.prototype.get = function(index) {
    if(index < this.linked.length){
        return this.linked[index];
    }
    return -1;
};

MyLinkedList.prototype.addAtHead = function(val) {
    this.linked.unshift(val);
};

MyLinkedList.prototype.addAtTail = function(val) {
    this.linked.push(val);
};

MyLinkedList.prototype.addAtIndex = function(index, val) {
    if(index <= this.linked.length){
        this.linked.splice(index,0,val);
    }
};

MyLinkedList.prototype.deleteAtIndex = function(index) {
    this.linked.splice(index,1);
};
Design Linked List