# 707. Design Linked List
Difficulty: Medium
## Solution
```cpp=
/**
*** Author: R-CO
*** E-mail: daniel1820kobe@gmail.com
*** Date: 2020-09-16
**/
#include <cstdlib>
#include <iostream>
#include <memory>
using namespace std;
#define WINDOWS 1
struct DoublyNode {
DoublyNode(int val) {
this->val = val;
}
int val{ -1 };
DoublyNode *prev{ nullptr };
DoublyNode *next{ nullptr };
};
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList() {
}
~MyLinkedList() {
for (auto *ptr = head_; ptr != nullptr; ptr = head_) {
head_ = head_->next;
delete ptr;
}
head_ = nullptr;
tail_ = nullptr;
}
/** 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 >= length_ || index < 0) {
return -1;
}
auto *ptr = FindNode(index);
int value = ptr->val;
return value;
}
/** 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 addAtHead(int val) {
++length_;
if (head_ == nullptr) {
head_ = new DoublyNode(val);
tail_ = head_;
return;
}
head_->prev = new DoublyNode(val);
head_->prev->next = head_;
head_ = head_->prev;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
++length_;
if (tail_ == nullptr) {
tail_ = new DoublyNode(val);
head_ = tail_;
return;
}
tail_->next = new DoublyNode(val);
tail_->next->prev = tail_;
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. */
void addAtIndex(int index, int val) {
if (index > length_ || index < 0) {
return;
}
if (index == length_) {
addAtTail(val);
return;
}
DoublyNode *ptr = FindNode(index);
auto *prev = ptr->prev;
ptr->prev = new DoublyNode(val);
ptr->prev->prev = prev;
ptr->prev->next = ptr;
++length_;
if (prev != nullptr) {
prev->next = ptr->prev;
}
else {
head_ = ptr->prev;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if (index >= length_ || index < 0) {
return;
}
auto *ptr = FindNode(index);
auto *prev = ptr->prev;
auto *next = ptr->next;
if (ptr == head_) {
head_ = next;
}
if (ptr == tail_) {
tail_ = prev;
}
delete ptr;
ptr = nullptr;
--length_;
if (prev != nullptr) {
prev->next = next;
}
if (next != nullptr) {
next->prev = prev;
}
}
void PrintNodes() {
for (auto *ptr = head_; ptr != nullptr; ptr = ptr->next) {
cout << ptr->val << " ";
}
cout << endl;
}
private:
DoublyNode *FindNode(int index) {
DoublyNode *ptr = nullptr;
/*int reverse_index = length_ - index - 1;
if (reverse_index < index) {
ptr = SearchFromTail(reverse_index);
}
else {
ptr = SearchFromHead(index);
}*/
ptr = SearchFromHead(index);
return ptr;
}
DoublyNode* SearchFromHead(int index) {
auto *ptr = head_;
while (index-- > 0) {
ptr = ptr->next;
}
return ptr;
}
DoublyNode* SearchFromTail(int reverse_index) {
auto *ptr = tail_;
while (reverse_index-- > 0) {
ptr = ptr->prev;
}
return ptr;
}
int length_{ 0 };
DoublyNode *head_{ nullptr };
DoublyNode *tail_{ nullptr };
};
/**
* 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);
*/
int main(int argc, char *argv[])
{
//auto *linkedList = new MyLinkedList(); // Initialize empty LinkedList
//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
//delete linkedList;
//
////auto *li = new MyLinkedList(); // Initialize empty LinkedList
//auto li = make_unique<MyLinkedList>();
//li->addAtHead(7);
//li->addAtTail(7);
//li->addAtHead(9);
//li->addAtTail(8);
//li->addAtHead(6);
//li->addAtHead(0);
//li->get(5); // 8
//li->addAtHead(0);
//li->get(2); // 6
//li->get(5); // 7
//li->addAtTail(4);
/*["MyLinkedList", "addAtHead", "addAtIndex", "addAtTail", "addAtHead", "addAtIndex", "addAtTail", "addAtTail", "addAtIndex", "deleteAtIndex", "deleteAtIndex", "addAtTail"]
[[], [0], [1, 4], [8], [5], [4, 3], [0], [5], [6, 3], [7], [5], [4]]*/
/*auto li_2 = make_unique<MyLinkedList>();
li_2->addAtHead(0);
li_2->PrintNodes();
li_2->addAtIndex(1, 4);
li_2->PrintNodes();
li_2->addAtTail(8);
li_2->PrintNodes();
li_2->addAtHead(5);
li_2->PrintNodes();
li_2->addAtIndex(4, 3);
li_2->PrintNodes();
li_2->addAtTail(0);
li_2->PrintNodes();
li_2->addAtTail(5);
li_2->PrintNodes();
li_2->addAtIndex(6, 3);
li_2->PrintNodes();
li_2->deleteAtIndex(7);
li_2->PrintNodes();
li_2->deleteAtIndex(5);
li_2->PrintNodes();
li_2->addAtTail(4);
li_2->PrintNodes();*/
/*["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]*/
auto li_3 = make_unique<MyLinkedList>();
li_3->addAtHead(1);
li_3->PrintNodes();
li_3->addAtTail(3);
li_3->PrintNodes();
li_3->addAtIndex(1, 2);
li_3->PrintNodes();
li_3->get(1);
li_3->PrintNodes();
li_3->deleteAtIndex(1);
li_3->PrintNodes();
li_3->get(1);
li_3->PrintNodes();
#if WINDOWS
_CrtDumpMemoryLeaks();
#endif
return EXIT_SUCCESS;
}
```
## Result
Success
Details
Runtime: 76 ms, faster than 58.77% of C++ online submissions for Design Linked List.
Memory Usage: 20 MB, less than 5.07% of C++ online submissions for Design Linked List.
###### tags: `LeetCode-Medium` `C++`