[leetcode]707. Design Linked List

[leetcode]707. Design Linked List


Analysis

年纪大了就会比较惜命~—— [ummmm~]

Design your implementation of the linked list. You can choose to use the singly linked list or the 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 these functions in your linked list class:
get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
addAtHead(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.
addAtTail(val) : Append a node of value val to the last element of the linked list.
addAtIndex(index, val) : 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.
deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.
实现一个链表,可以是单链表,也可以是双链表。我用的是vector,看到评论区也有用结构体的。

Implement

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

    MyLinkedList() {

    }

    /** 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 >= myList.size())
            return -1;
        return myList[index];
    }

    /** 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) {
        myList.insert(myList.begin(), val);
    }

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

    /** 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 > myList.size())
           return ;
       else if(index == myList.size())
           myList.push_back(val);
       else
           myList.insert(myList.begin()+index, val);
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index >= myList.size())
            return ;
        myList.erase(myList.begin()+index);
    }
private:
    vector<int> myList;
};

/**
 * 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);
 */

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/81169738