Leetcode Index

707.Design Linked List

707.Design Linked List

难度:Easy

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.

Implement these functions in your linked list class:

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. deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:
MyLinkedList linkedList = new MyLinkedList();
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
Note:
All values will be in the range of [1, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in LinkedList library.

需要注意增加删除节点时的逻辑性,如在某个节点后添加节点,需要先处理完这个节点之后的节点,才能将cur->next=newNode,同理删除节点也是一样。

class List{
public:
int val;
List* next;
List* pre;
List(int v, List* n, List* p ):val(v),next(n),pre(p){}
};
class MyLinkedList {
public:
List* root;
List* trail;
int size;
/** Initialize your data structure here. */
MyLinkedList() {
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. */
void addAtHead(int val) {
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. */
void addAtTail(int val) {
if(size==0)
{
addAtHead(val);
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)
{
addAtHead(val);
return;
}
if(index == size) {
addAtTail(val);
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--;
}
};
/**
* 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);
**/

执行用时 :96 ms, 在所有 C++ 提交中击败了34.36%的用户 内存消耗 :19.5 MB, 在所有 C++ 提交中击败了35.99%的用户