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

  1. Input
  2. ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
  3. [[], [1], [3], [1, 2], [1], [1], [1]]
  4. Output
  5. [null, null, null, null, 2, null, 3]
  6. Explanation
  7. MyLinkedList myLinkedList = new MyLinkedList();
  8. myLinkedList.addAtHead(1);
  9. myLinkedList.addAtTail(3);
  10. myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
  11. myLinkedList.get(1); // return 2
  12. myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
  13. myLinkedList.get(1); // return 3

提示:

  • 所有val值都在 [1, 1000] 之内;
  • 操作次数将在 [1, 1000] 之内;
  • 请不要使用内置的 LinkedList 库;

思路

考验读者代码实现的能力。当没有思路的时候一定要画图。注意边界处理(比如没节点的时候调用addAtIndex(0,9)应该怎么处理)。

代码

Cpp:

  1. // 44ms, 18.9MB
  2. class MyLinkedList {
  3. public:
  4. typedef struct Node {
  5. int val;
  6. Node* next;
  7. Node(int x): val(x), next(nullptr) {}
  8. }Node;
  9. /** Initialize your data structure here. */
  10. MyLinkedList() {
  11. this->dummy_head = new Node(0);
  12. this->size = 0;
  13. }
  14. /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
  15. int get(int index) {
  16. if( index < 0 || index > (this->size - 1) ) {
  17. cout << "Bad index! at get()" << endl;
  18. return -1;
  19. }
  20. Node* cur_node = this->dummy_head->next;
  21. while( index-- ) {
  22. cur_node = cur_node->next;
  23. }
  24. return cur_node->val;
  25. }
  26. /** 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. */
  27. void addAtHead(int val) {
  28. Node* new_node = new Node(val);
  29. new_node->next = this->dummy_head->next;
  30. dummy_head->next = new_node;
  31. this->size += 1;
  32. }
  33. /** Append a node of value val to the last element of the linked list. */
  34. void addAtTail(int val) {
  35. Node* new_node = new Node(val);
  36. Node* cur_node = this->dummy_head;
  37. while( cur_node->next != nullptr ) {
  38. cur_node = cur_node->next;
  39. }
  40. cur_node->next = new_node;
  41. this->size += 1;
  42. }
  43. /** 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. */
  44. void addAtIndex(int index, int val) {
  45. if( index > this->size ) {
  46. cout << "Bad index at addAtIndex()" << endl;
  47. return;
  48. }
  49. Node* new_node = new Node(val);
  50. Node* cur_node = this->dummy_head;
  51. while( index-- ) {
  52. cur_node = cur_node->next;
  53. }
  54. new_node->next = cur_node->next;
  55. cur_node->next = new_node;
  56. this->size += 1;
  57. }
  58. /** Delete the index-th node in the linked list, if the index is valid. */
  59. void deleteAtIndex(int index) {
  60. if( index < 0 || index >= this->size ) {
  61. cout << "Bad index at deleteAtIndex()" << endl;
  62. return;
  63. }
  64. Node* cur_node = this->dummy_head;
  65. while( index-- ) {
  66. cur_node = cur_node->next;
  67. }
  68. Node* tmp = cur_node->next;
  69. cur_node->next = cur_node->next->next;
  70. delete tmp;
  71. this->size -= 1;
  72. }
  73. private:
  74. int size;
  75. Node* dummy_head;
  76. };
  77. /**
  78. * Your MyLinkedList object will be instantiated and called as such:
  79. * MyLinkedList* obj = new MyLinkedList();
  80. * int param_1 = obj->get(index);
  81. * obj->addAtHead(val);
  82. * obj->addAtTail(val);
  83. * obj->addAtIndex(index,val);
  84. * obj->deleteAtIndex(index);
  85. */