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.
Example:
Note:
All values will be in the range of [1, 1000].
The number of operations will be in the range of [1, 1000].

class List{
public:
int val;
List* next;
List* pre;
List(int v, List* n, List* p ):val(v),next(n),pre(p){}
};
public:
List* root;
List* trail;
int size;
/** Initialize your data structure here. */
root=nullptr;
trail = nullptr;
size=0;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if(index<0 || index>=size) return -1;
List* cur=root;
// cout<<index<< " "<<size<<endl;
while(index--)
{
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. */
if(size==0)
{
root = new List(val, nullptr,nullptr);
trail=root;
}
else
{
List* newNode = new List(val, root,nullptr);
root->pre= newNode;
root=newNode;
}
size++;
}
/** Append a node of value val to the last element of the linked list. */
if(size==0)
{
return;
}
List *newNode = new List(val, nullptr, trail);
trail->next = newNode;
trail = newNode;
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. */
void addAtIndex(int index, int val) {
// cout<<size<<endl;
if(index > size) return;
if(index<=0)
{
return;
}
if(index == size) {
return;
}
List* cur = root;
while(--index)
{
cur = cur->next;
}
// cout<<cur->val<<endl;
List* newNode = new List(val, cur->next, cur);
cur->next->pre = newNode;
cur->next=newNode;
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
// cout<<size<<" "<<index<<endl;
if(index <0 || size==0 || index>size-1) return;
if(size ==1 )
{
List* tmp=root;
root=nullptr;
trail=nullptr;
delete tmp;
size--;
return;
}
if(index ==0)
{
List* old=root;
root=root->next;
root->pre=nullptr;
size--;
delete old;
return;
}
if(index == size -1)
{
List* old=trail;
trail = trail->pre;
trail->next =nullptr;
size--;
delete old;
return;
}
List* cur=root;
while(--index)
{
cur=cur->next;
}
List* tmp=cur->next;
cur->next->next->pre= cur;
cur->next = cur->next->next;
delete tmp;
size--;
}
};
/**