707 - Design Linked List
#medium
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 theMyLinkedListobject.int get(int index)Get the value of theindexthnode in the linked list. If the index is invalid, return-1.void addAtHead(int val)Add a node of valuevalbefore 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 valuevalas the last element of the linked list.void addAtIndex(int index, int val)Add a node of valuevalbefore theindexthnode in the linked list. Ifindexequals the length of the linked list, the node will be appended to the end of the linked list. Ifindexis greater than the length, the node will not be inserted.void deleteAtIndex(int index)Delete theindexthnode in the linked list, if the index is valid.
0 <= index, val <= 1000- Please do not use the built-in LinkedList library.
- At most
2000calls will be made toget,addAtHead,addAtTail,addAtIndexanddeleteAtIndex.
struct LinkNode {
int val;
LinkNode * next;
};
class MyLinkedList {
public:
LinkNode * node = NULL; // for leetcode to make sure the init value is NULL.
MyLinkedList() {
}
int get(int index) {
LinkNode * it_node = node;
for ( int i = 0; it_node != NULL; i++ ) {
if ( i == index ) return it_node -> val;
it_node = it_node -> next;
}
return -1;
}
void addAtHead(int val) {
if ( node == NULL ) {
node = new LinkNode;
node -> val = val;
node -> next = NULL;
}
else {
LinkNode * t_node = new LinkNode;
t_node -> val = val;
t_node -> next = node;
// alert: 記得把舊頭指向新頭。
node = t_node;
}
}
void addAtTail(int val) {
if ( node == NULL ) {
node = new LinkNode;
node -> val = val;
node -> next = NULL;
}
else {
LinkNode * t_node = new LinkNode;
t_node -> val = val;
t_node -> next = NULL;
LinkNode * it_node = node;
while ( it_node != NULL ) {
if ( it_node -> next == NULL ) {
it_node -> next = t_node;
return;
}
it_node = it_node -> next;
}
}
}
void addAtIndex(int index, int val) {
// alert: 注意node = null但想取index > 0 的狀況,這種不加。
if ( node == NULL && index == 0 ) {
node = new LinkNode;
node -> val = val;
node -> next = NULL;
}
else {
LinkNode * t_node = new LinkNode;
t_node -> val = val;
t_node -> next = NULL;
LinkNode * it_node = node;
for ( int i = 0; it_node != NULL; i++ ) {
// 考慮插頭/中間
if ( index == 0 ) {
t_node -> next = node;
node = t_node;
return;
}
else if ( i + 1 == index ) {
if ( it_node -> next != NULL ) {
LinkNode * t2_node = it_node -> next;
it_node -> next = t_node;
t_node -> next = t2_node;
return;
}
// If index equals the length of the linked list, the node will be appended to the end of the linked list.
else {
it_node -> next = t_node;
return;
}
}
else {
it_node = it_node -> next;
}
}
}
}
void deleteAtIndex(int index) {
if ( node != NULL ) {
LinkNode * it_node = node;
for ( int i = 0; it_node != NULL; i++ ) {
// 一樣考慮頭/中間/尾巴
if ( index == 0 ) {
if ( node -> next != NULL ) node = node -> next;
else node = NULL;
return;
}
else if ( i + 1 == index ) {
if ( it_node -> next != NULL ) {
if ( it_node -> next -> next != NULL ) {
it_node -> next = it_node -> next -> next;
}
else {
it_node -> next = NULL;
}
return;
}
else {
it_node = it_node -> next;
}
}
else {
it_node = it_node -> next;
}
}
}
}
};
/**
* 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);
*/