跳转至

707.设计链表 (Medium)*

题目描述*

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

示例*

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

代码*

菜鸡菜到还没自己实现过链表,正好用这个试一下,总用 STL 还是不行。

肯定是先定义一个结点类,然后 MyLinkedList 对头结点进行处理。开始我竟然还想直接在链表类里放数据,脑子瓦特了。

因为有在尾部插入,所以添加了尾指针,指向最后一个元素,链表为空时指针为空。

// Definition of linked list node
struct myListNode {
    int val;
    myListNode *next;
    myListNode() : val(-1), next(nullptr) {}
    myListNode(int x) : val(x), next(nullptr) {}
};

class MyLinkedList {
public:
    myListNode *head;
    myListNode *tail;
    int length;
    /** Initialize your data structure here. */
    MyLinkedList(): length(0) {
        head = new myListNode();
        tail = nullptr;
    }

    /** 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 > length - 1 || index < 0){
            return -1;
        }
        myListNode *p = head->next;
        while(index--) {
            p = p->next;
        }
        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 *cur = new myListNode(val);
        // 如果是第一次添加,那么尾指针指向新结点
        if(tail == nullptr) {
            tail = cur;
        }
        cur->next = head->next;
        head->next = cur;
        length++;
    }

    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        // 如果尾指针为空,即链表为空
        if(tail == nullptr) {
            addAtHead(val);
        }else {
            myListNode *cur = new myListNode(val);
            tail->next = cur;
            tail = cur;
            length++;
        }
    }

    /** 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 > length) {
            return;
        }else if(index == length) {
            addAtTail(val);
        }else if(index < 0){
            addAtHead(val);
            return;
        }else {
            myListNode *cur = new myListNode(val);
            myListNode *p = head;
            while(index--) {
                p = p->next;
            }
            cur->next = p->next;
            p->next = cur;
            length++;
        }
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index > length - 1 || index < 0) {
            return;
        }
        myListNode *p = head;
        while(index--) {
            p = p->next;
        }
        myListNode *cur = p->next;
        p->next = p->next->next;
        // 如果删除的是尾结点,要更新尾指针。
        length--;
        delete cur;
        if(p->next == nullptr) {
            tail = p;
        }
    }
};

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

最后更新: July 23, 2022