1. 程式人生 > 實用技巧 >707. 設計連結串列

707. 設計連結串列

設計連結串列的實現。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節點應該具有兩個屬性:val和next。val是當前節點的值,next是指向下一個節點的指標/引用。如果要使用雙向連結串列,則還需要一個屬性prev以指示連結串列中的上一個節點。假設連結串列中的所有節點都是 0-index 的。

在連結串列類中實現這些功能:

get(index):獲取連結串列中第index個節點的值。如果索引無效,則返回-1。
addAtHead(val):在連結串列的第一個元素之前新增一個值為val的節點。插入後,新節點將成為連結串列的第一個節點。
addAtTail(val):將值為val 的節點追加到連結串列的最後一個元素。
addAtIndex(index,val):在連結串列中的第index個節點之前新增值為val 的節點。如果index等於連結串列的長度,則該節點將附加到連結串列的末尾。如果 index 大於連結串列長度,則不會插入節點。如果index小於0,則在頭部插入節點。
deleteAtIndex(index):如果索引index 有效,則刪除連結串列中的第index 個節點。

示例:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //連結串列變為1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //現在連結串列是1-> 3
linkedList.get(1); //返回3

提示:

所有val值都在[1, 1000]之內。
操作次數將在[1, 1000]之內。
請不要使用內建的 LinkedList 庫。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/design-linked-list
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

class Node{
    public:
        int val;
        Node* next;
        Node(int val){
            this->val=val;
            this->next=NULL;
        }
};
class MyLinkedList {
public:
    /** Initialize your data structure here. 
*/ MyLinkedList() { head=tail=NULL; length=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>=length)return -1; Node* tmp=head; int cnt=0; while(cnt<index){ tmp=tmp->next; cnt++; } return tmp->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) { Node* tmp=new Node(val); tmp->next=head; head=tmp; length++; if(length==1)tail=head; } /** Append a node of value val to the last element of the linked list. */ void addAtTail(int val) { Node* tmp=new Node(val); if(tail!=nullptr)tail->next=tmp; tail=tmp; length++; if(length==1)head=tail; } /** 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<0||index>length)return; if(index==0){ addAtHead(val); return; } if(index==length){ addAtTail(val); return; } Node* tmp=head; for(int i=0;i<index-1;i++) tmp=tmp->next; Node* tmp_add=new Node(val); tmp_add->next=tmp->next; tmp->next=tmp_add; length++; } /** Delete the index-th node in the linked list, if the index is valid. */ void deleteAtIndex(int index) { if(index<0||index>=length)return; if(index==0){ Node* del=head; head=head->next; if(head==tail)tail=nullptr; delete del; length--; return; } Node* tmp=head; for(int i=0;i<index-1;i++) tmp=tmp->next; Node* del=tmp->next; tmp->next=del->next; if(index==length-1)tail=tmp; delete del; length--; } private: int length; Node* head; Node* tail; }; /** * 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); */