C++: Leetcode-Linked List-707 Design Linked List

C++: Leetcode-Linked List-707 Design Linked List

Familiar with various operations of consolidating linked lists



topic

Design the implementation of linked list. You can choose to use a singly linked list or a 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 a doubly linked list, you also need an attribute prev to indicate the previous node in the linked list. Assume that all nodes in the linked list are 0-index.

Implement these functions in the linked list class:

get(index): Get the value of the index node in the linked list. Returns -1 if the index is invalid.
addAtHead(val): Add a node whose value is val before the first element of the linked list. After insertion, the new node will be the first node of the linked list.
addAtTail(val): Append the node whose value is val to the last element of the linked list.
addAtIndex(index,val): Add a node with the value val before the index node in the linked list. If index is equal to 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 of the linked list, the node will not be inserted. If index is less than 0, insert the node at the head.
deleteAtIndex(index): If the index index is valid, delete the index node in the linked list.


Idea analysis

1. Create node
2. Obtain node value from index
3. Add head node
4. Add tail node
5. Insert node
6. Delete node

Note that the index starts from 0, and the linkedSize is the actual length. Don’t forget to insert the node. Just remember to update the linkedSize.

the code

/*
Leetcode-707设计链表
自行设计链表的一些功能,加强对链表的理解与运用能力

1、创建节点
2、索引获取节点值
3、添加头结点
4、添加尾节点
5、插入节点
6、删除节点

此题注意index是从0开始,linkedSize是实际长度,插入节点别漏情况,记得更新linkedSize即可
*/

#include "iostream"
#include "vector"
using namespace std;

class MyLinkedList
{
    
    
private:
    /* data */
public:
    //创建节点
    class linkedNode
    {
    
    
    public:
        int val;
        linkedNode *next;
        linkedNode() : val(0), next(nullptr) {
    
    }
        linkedNode(int x) : val(x), next(nullptr) {
    
    }
        linkedNode(int x, linkedNode *l_next) : val(x), next(l_next) {
    
    }
    };
    linkedNode *dummyHead; //虚拟头结点
    int linkedSize;
    //初始化虚拟头结点
    MyLinkedList()
    {
    
    
        dummyHead = new linkedNode(0);
        linkedSize = 0;
    }

    //初始化链表
    linkedNode *createList(vector<int> &nums)
    {
    
    
        linkedNode *head = new linkedNode(nums[0]);
        linkedNode *p = head;
        for (int i = 1; i < nums.size(); i++)
        {
    
    
            p->next = new linkedNode(nums[i]);
            p = p->next;
        }
        //     delete p; //不能delete,有bug,当没有进入for,p的内存空间可能跟head一样,也会把head空间删除掉,return head出错!!!!!
        linkedSize += nums.size();
        dummyHead->next = head;
        return head; //返回头节点
    }

    //索引获取节点值
    //获取链表中第 index 个节点的值
    //注意:index是从0开始,linkedSize就是实际长度
    int get(int index)
    {
    
    
        if (index > (linkedSize - 1) || index < 0)
        {
    
    
            return -1;
        }
        linkedNode *current = dummyHead->next; //指向头节点
        while (index--)
        {
    
    
            current = current->next;
        }
        return current->val;
    }

    //添加头结点
    void addAtHead(int val)
    {
    
    
        linkedNode *newHead = new linkedNode(val);
        newHead->next = dummyHead->next; //新头结点指向老头结点
        dummyHead->next = newHead;       //虚拟头结点指向新头结点
        linkedSize++;
    }

    //添加尾节点
    void addAtTail(int val)
    {
    
    
        linkedNode *newTail = new linkedNode(val);
        linkedNode *current = dummyHead;

        while (current->next != nullptr)
        {
    
    
            current = current->next;
        }
        current->next = newTail;
        linkedSize++;
    }

    //插入节点
    //在链表中的第 index 个节点之前添加值为 val 的节点
    void addAtIndex(int index, int val)
    {
    
    
        linkedNode *newNoed = new linkedNode(val);
        if (index == linkedSize) //节点插入末尾

        {
    
    
            addAtTail(val);
        }
        else if (index > linkedSize) //不插入节点

        {
    
    
            return;
        }

        else if (index == 0) //插入头部
        {
    
    
            addAtHead(val);
        }
        else //插入中间节点
        {
    
    
            linkedNode *current = dummyHead;
            while (index--)
            {
    
    
                current = current->next;
            }
            newNoed->next = current->next;
            current->next = newNoed;
            linkedSize++;
        }
    }

    //删除节点
    //删除链表中的第 index 个节点。
    //index从0开始,第0节点为头结点
    //linkedSize为实际长度
    void deleteAtIndex(int index)
    {
    
    
        //索引无效或没有节点
        if (index < 0 || index >= linkedSize)
        {
    
    
            return;
        }
        else
        {
    
    
            linkedNode *current = dummyHead; // 现节点为虚拟节点,即头结点的上一个节点,方便后续寻找index的上一个节点
            //遍历寻找第index个节点的上一个节点
            while (index--)
            {
    
    
                current = current->next;
            }
            linkedNode *temp = current->next; //赋值给temp,方便delete
            current->next = current->next->next;
            delete temp;
            linkedSize--;
        }
    }

    //打印链表
    void printlist()
    {
    
    
        linkedNode *current = dummyHead->next; //现节点为头结点
        while (current != nullptr)             //注意不是current->next!=nullptr ,因为尾节点的指针指向nullptr
        {
    
    
            cout << current->val << "\t";
            current = current->next;
        }
        cout << endl;
    }
};

int main(int argc, const char **argv)
{
    
    
    vector<int> nums = {
    
    1};
    MyLinkedList mylist;
    mylist.createList(nums);
    mylist.printlist();

    mylist.addAtTail(3);
    mylist.printlist();

    mylist.addAtIndex(1, 2); //有问题
    mylist.printlist();

    cout << mylist.get(1) << endl; //有问题

    mylist.deleteAtIndex(1);
    mylist.printlist();

    cout << mylist.get(1) << endl;

    return 0;
}

Summarize

Brush questions to consolidate the mastery of linked lists, and further strengthen the use and understanding of pointers
Reference code random thoughts
https://www.programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE %E8%A1%A8.html#%E4%BB%A3%E7%A0%81

Guess you like

Origin blog.csdn.net/Bellwen/article/details/128422061