707. Design Linked List

Design your implementation of the linked list. You can choose to use a singly or 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 the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
  • void addAtHead(int 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 addAtTail(int val) Append a node of value val as the last element of the linked list.
  • void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
  • void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.

Example 1:

复制代码
Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]

Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2);    // linked list becomes 1->2->3
myLinkedList.get(1);              // return 2
myLinkedList.deleteAtIndex(1);    // now the linked list is 1->3
myLinkedList.get(1);              // return 3

Constraints:

  • 0 <= index, val <= 1000

  • Please do not use the built-in LinkedList library.

  • At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.

    class MyLinkedList {
    public:
    struct ListNode
    {
    int val;
    ListNode*next;
    ListNode(int val):val(val),next(nullptr){}
    };
    //初始化链表
    MyLinkedList() {
    _dummyHead=new ListNode(0);
    _size=0;
    }

      int get(int index) {
          if(index<0 || index>(_size-1))return -1;
          ListNode*curr=_dummyHead->next;
          while(index--)curr=curr->next;
          return curr->val;
      }
      
      void addAtHead(int val) {
          ListNode*curr=new ListNode(val);
          curr->next=_dummyHead->next;
          _dummyHead->next=curr;
          _size++;
      }
      
      void addAtTail(int val) {
          ListNode*newnode=new ListNode(val);
          ListNode*curr=_dummyHead;
          while(curr->next!=NULL)curr=curr->next;
          curr->next=newnode;
          _size++;
      }
      
      void addAtIndex(int index, int val) {
          if(index<0)index=0;
          if(index>_size)return;
          ListNode*newnode=new ListNode(val);
          ListNode*curr=_dummyHead;
          while(index--)curr=curr->next;
          newnode->next=curr->next;
          curr->next=newnode;
          _size++;
      }
      
      void deleteAtIndex(int index) {
          if(index>=_size || index<0)return;
          ListNode*curr=_dummyHead;
          while(index--)curr=curr->next;
          curr->next=curr->next->next;
          _size--;
      }
      void PrintLinedList(){
          ListNode*curr=_dummyHead;
          while(curr->next!=NULL){
              cout<<curr->next->val<<" ";
              curr=curr->next;
          }
          cout<<endl;
      }
    

    private:
    int _size;
    ListNode*_dummyHead;
    };

注意:

1.其实这里使用到的各种链表的操作都不是很难,只是需要注意index下标是从0开始的。考虑各种极端一点的情况。

2.关于初始化链表和struct ListNode方面我还有一点欠缺

相关推荐
肥猪猪爸28 分钟前
使用卡尔曼滤波器估计pybullet中的机器人位置
数据结构·人工智能·python·算法·机器人·卡尔曼滤波·pybullet
linux_carlos29 分钟前
环形缓冲区
数据结构
readmancynn40 分钟前
二分基本实现
数据结构·算法
Bucai_不才44 分钟前
【数据结构】树——链式存储二叉树的基础
数据结构·二叉树
盼海1 小时前
排序算法(四)--快速排序
数据结构·算法·排序算法
一直学习永不止步1 小时前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表
Rstln2 小时前
【DP】个人练习-Leetcode-2019. The Score of Students Solving Math Expression
算法·leetcode·职场和发展
珹洺2 小时前
C语言数据结构——详细讲解 双链表
c语言·开发语言·网络·数据结构·c++·算法·leetcode
几窗花鸢2 小时前
力扣面试经典 150(下)
数据结构·c++·算法·leetcode
.Cnn2 小时前
用邻接矩阵实现图的深度优先遍历
c语言·数据结构·算法·深度优先·图论