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.

• 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. If index is negative, the node will be inserted at the head of the list.
• deleteAtIndex(index) : Delete the `index`-th node in the linked list, if the index is valid.

Example:

``````MyLinkedList linkedList = new MyLinkedList();
``````

Note:

• All values will be in the range of `[1, 1000]`.
• The number of operations will be in the range of `[1, 1000]`.

``````class MyLinkedList {
public:
size = 0;
}
int get(int index) {
if (index < 0 || index >= size) return -1;
for (int i = 0; i < index; ++i) cur = cur->next;
return cur->val;
}
Node *t = new Node(val, head);
++size;
}
while (cur->next) cur = cur->next;
cur->next = new Node(val, NULL);
++size;
}
void addAtIndex(int index, int val) {
if (index > size) return;
for (int i = 0; i < index - 1; ++i) cur = cur->next;
Node *t = new Node(val, cur->next);
cur->next = t;
++size;
}
void deleteAtIndex(int index) {
if (index < 0 || index >= size) return;
if (index == 0) {
--size;
return;
}
for (int i = 0; i < index - 1; ++i) cur = cur->next;
cur->next = cur->next->next;
--size;
}

private:
struct Node {
int val;
Node *next;
Node(int x, Node* n): val(x), next(n) {}
};
int size;
};
``````

``````class MyLinkedList {
public:
head = NULL; tail = NULL;
size = 0;
}
int get(int index) {
if (index < 0 || index >= size) return -1;
for (int i = 0; i < index; ++i) cur = cur->next;
return cur->val;
}
Node *t = new Node(val, head);
if (size == 0) tail = t;
++size;
}
Node *t = new Node(val, NULL);
if (size == 0) {tail = t; head = t;}
tail->next = t;
tail = t;
++size;
}
void addAtIndex(int index, int val) {
if (index > size) return;
if (index == size) {addAtTail(val); return;}
for (int i = 0; i < index - 1; ++i) cur = cur->next;
Node *t = new Node(val, cur->next);
cur->next = t;
++size;
}
void deleteAtIndex(int index) {
if (index < 0 || index >= size) return;
if (index == 0) {
--size;
return;
}
for (int i = 0; i < index - 1; ++i) cur = cur->next;
cur->next = cur->next->next;
if (index == size - 1) tail = cur;
--size;
}

private:
struct Node {
int val;
Node *next;
Node(int x, Node* n): val(x), next(n) {}
};
int size;
};
``````

``````class MyLinkedList {
public:

int get(int index) {
return (index >= 0 && index < data.size()) ? data[index] : -1;
}
data.push_front(val);
}
data.push_back(val);
}
void addAtIndex(int index, int val) {
if (index > (int)data.size()) return;
data.insert(data.begin() + index, val);
}
void deleteAtIndex(int index) {
if (index < 0 || index >= data.size()) return;
data.erase(data.begin() + index);
}

private:
deque<int> data;
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/707