LeetCode | 707. 設計鏈表

設計鏈表的實現。您能夠選擇使用單鏈表或雙鏈表。單鏈表中的節點應該具備兩個屬性:valnextval是當前節點的值,next是指向下一個節點的指針/引用。若是要使用雙向鏈表,則還須要一個屬性 prev以指示鏈表中的上一個節點。假設鏈表中的全部節點都是 0-index的。node

在鏈表類中實現這些功能:app

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

提示:code

  • 全部val值都在$ [1, 1000] $以內。
  • 操做次數將在 $[1, 1000] $以內。
  • 請不要使用內置的 LinkedList 庫。

Code索引

struct myListNode
{
    int val;
    myListNode* next;
    myListNode() :val(-1), next(nullptr) {}
};


class MyLinkedList {
public:
    int m_size;
    myListNode* m_head;
    myListNode* m_tail;
    
public:
    /** Initialize your data structure here. */
    MyLinkedList() :m_size(0)
    {
        m_head = new myListNode();
    }

    /** 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 > m_size - 1 || index < 0)
        {
            return -1;
        }
        myListNode* p = m_head->next;
        while (index != 0)
        {
            p = p->next;
            --index;
        }
        //std::cout << p->val;
        return p->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)
    {
        myListNode* newNode = new myListNode();
        newNode->next = m_head->next;
        m_head->next = newNode;
        newNode->val = val;
        m_size += 1;
    }

    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val)
    {
        myListNode* lastNode = new myListNode();
        lastNode->val = val;
        myListNode* p = m_head;
        int len = m_size;
        while (len != 0)
        {
            p = p->next;
            --len;
        }
        p->next = lastNode;
        lastNode->next = NULL;
        m_size += 1;
    }

    /** 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 > m_size) return;
        if (index == m_size)
        {
            addAtTail(val);
            return;
        }
        if (index < 0)
        {
            addAtHead(val);
            return;
        }
        myListNode* newNode = new myListNode();
        myListNode* p = m_head;
        while (index != 0)
        {
            p = p->next;
            --index;
        }
        newNode->next = p->next;
        p->next = newNode;
        newNode->val = val;
        m_size += 1;
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index)
    {
        if (index > m_size - 1 || index < 0) return;
        myListNode* p = m_head;
        while (index != 0)
        {
            p = p->next;
            --index;
        }
        myListNode* delNode = p->next;;
        p->next = p->next->next;
        m_size -= 1;
        delete delNode;
    }
};
相關文章
相關標籤/搜索