题目链接

题目描述

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:valnextval 是当前节点的值,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 个节点。

    示例

    示例1:

    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 库。

    思路

    思路1:单链表

    设置一个虚拟头结点便于操作。

    思路2:双链表

    设置一个虚拟头结点和虚拟尾结点便于操作。
    一点优化:当要查找的 index 在链表的前半部分时,正向遍历;在后半部分时,反向遍历。

    题解

    思路1:单链表

    ```cpp class MyLinkedList { private: struct ListNode {

    1. int val;
    2. ListNode* next;
    3. ListNode(int val) : val(val), next(nullptr) {}

    }; int _size; ListNode* _dummyHead;

    public: /* Initialize your data structure here. / MyLinkedList() {

    1. _dummyHead = new ListNode(0);
    2. _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) {

    1. if (index >= _size || index < 0) {
    2. return -1;
    3. }
    4. ListNode* cur = _dummyHead->next;
    5. while (index--) {
    6. cur = cur->next;
    7. }
    8. 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) {

    1. ListNode* newNode = new ListNode(val);
    2. newNode->next = _dummyHead->next;
    3. _dummyHead->next = newNode;
    4. _size++;

    }

    /* Append a node of value val to the last element of the linked list. / void addAtTail(int val) {

    1. ListNode* newNode = new ListNode(val);
    2. ListNode* cur = _dummyHead;
    3. while (cur->next != nullptr) {
    4. cur = cur->next;
    5. }
    6. cur->next = newNode;
    7. _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) {

    1. if (index > _size) {
    2. return;
    3. }
    4. ListNode* newNode = new ListNode(val);
    5. ListNode* cur = _dummyHead;
    6. while (index--) {
    7. cur = cur->next;
    8. }
    9. newNode->next = cur->next;
    10. cur->next = newNode;
    11. _size++;

    }

    /* Delete the index-th node in the linked list, if the index is valid. / void deleteAtIndex(int index) {

    1. if (index >= _size || index < 0) {
    2. return;
    3. }
    4. ListNode* cur = _dummyHead;
    5. while (index--) {
    6. cur = cur->next;
    7. }
    8. ListNode* temp = cur->next;
    9. cur->next = cur->next->next;
    10. delete temp;
    11. _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); */ ```

    思路2:双链表

    ```cpp class MyLinkedList { private: struct ListNode {

    1. int val;
    2. ListNode *next, *prev;
    3. ListNode(int val) : val(val), next(nullptr), prev(nullptr) {}

    }; int _size; ListNode _dummyHead, _dummyTail;

    public: /* Initialize your data structure here. / MyLinkedList() {

    1. _dummyHead = new ListNode(0);
    2. _dummyTail = new ListNode(0);
    3. _dummyHead->next = _dummyTail;
    4. _dummyTail->prev = _dummyHead;
    5. _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) {

    1. if (index >= _size || index < 0) {
    2. return -1;
    3. }
    4. ListNode* cur = nullptr;
    5. // 从前或者从后遍历
    6. if (index <= (_size - 1) / 2) {
    7. cur = _dummyHead->next;
    8. while (index--) {
    9. cur = cur->next;
    10. }
    11. } else {
    12. index = _size - 1 - index;
    13. cur = _dummyTail->prev;
    14. while (index--) {
    15. cur = cur->prev;
    16. }
    17. }
    18. 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) {

    1. ListNode* newNode = new ListNode(val);
    2. newNode->next = _dummyHead->next;
    3. _dummyHead->next->prev = newNode;
    4. _dummyHead->next = newNode;
    5. newNode->prev = _dummyHead;
    6. _size++;

    }

    /* Append a node of value val to the last element of the linked list. / void addAtTail(int val) {

    1. ListNode* newNode = new ListNode(val);
    2. newNode->prev = _dummyTail->prev;
    3. _dummyTail->prev->next = newNode;
    4. _dummyTail->prev = newNode;
    5. newNode->next = _dummyTail;
    6. _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) {

    1. if (index > _size) {
    2. return;
    3. }
    4. ListNode* newNode = new ListNode(val);
    5. ListNode* cur = nullptr;
    6. if (index <= (_size - 1) / 2) {
    7. cur = _dummyHead;
    8. while (index--) {
    9. cur = cur->next;
    10. }
    11. } else {
    12. index = _size - index;
    13. cur = _dummyTail;
    14. while (index--) {
    15. cur = cur->prev;
    16. }
    17. // 当我们找到要插入的位置后,再往前找一个,这样就可以跟从前遍历统一形式
    18. // 当然直接修改prev结点也是可以的
    19. cur = cur->prev;
    20. }
    21. newNode->next = cur->next;
    22. cur->next->prev = newNode;
    23. cur->next = newNode;
    24. newNode->prev = cur;
    25. _size++;

    }

    /* Delete the index-th node in the linked list, if the index is valid. / void deleteAtIndex(int index) {

    1. if (index >= _size || index < 0) {
    2. return;
    3. }
    4. ListNode* cur = nullptr;
    5. if (index <= (_size - 1) / 2) {
    6. cur = _dummyHead;
    7. while (index--) {
    8. cur = cur->next;
    9. }
    10. } else {
    11. index = _size - index;
    12. cur = _dummyTail;
    13. while (index--) {
    14. cur = cur->prev;
    15. }
    16. cur = cur->prev;
    17. }
    18. ListNode* temp = cur->next;
    19. cur->next->next->prev = cur;
    20. cur->next = cur->next->next;
    21. delete temp;
    22. _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); */ ```

    复杂度分析

    思路1:单链表

  • 时间复杂度:
  1. addAtHead0707-设计链表 - 图1
  2. get, addAtIndex, deleteAtIndex0707-设计链表 - 图20707-设计链表 - 图3指的元素索引。
  3. addAtTail0707-设计链表 - 图4
  • 空间复杂度:0707-设计链表 - 图5

    思路2:双链表

  • 时间复杂度:

  1. addAtHead, addAtTail0707-设计链表 - 图6
  2. get, addAtIndex, deleteAtIndex0707-设计链表 - 图70707-设计链表 - 图8指的元素索引。
  • 空间复杂度:0707-设计链表 - 图9