Introduction to linked list (C + +)

Keywords: C++ data structure linked list

Learn the basic knowledge of linked list on LeetCode and summarize as follows:

1. Introduction to single linked list

        Unlike an array, a single linked list only stores the value of the element. In addition to the value of the data, a single linked list also includes a reference field pointing to the next node, which is usually represented by next. As shown in the figure below, through this reference, the single linked list organizes all nodes in order.

      Generally, the single linked list is defined as follows:  

// Definition for singly-linked list.
struct SinglyListNode {
    int val;
    SinglyListNode *next;
    SinglyListNode(int x) : val(x), next(NULL) {}
};

        Unlike arrays, we can't randomly access the elements in the linked list, but if we want to get the ith element, we need to traverse from the beginning of the pointer. The average time complexity is O(N).

2. Add linked list

      Linked list addition is divided into middle addition, head addition and tail addition. The first is head addition:

      The head node is the representative of the whole linked list. Therefore, when adding nodes to the head, the most important thing is to update the head after adding:

  1. Initialize a cur;
  2. Connect the node to the head;
  3. Specify cur as head

 

 

  Add in the middle:

  1. First initialize cur

  2. Connect cur - > next to pred's next node, PRED - > next
  3. Finally, connect the broken PRED - > next to cur.

      In this way, compared with the array, we only need the time complexity of O(1) to insert the elements into the linked list.

  3. Delete

      If we want to delete a cur node, we need two steps:

  1. Find the previous node and the next node of cur;
  2. Connect the previous node pred to the next node cur

        Because the next node of the cur node is cur - > next, but the previous node can be found only after traversal, the time complexity of deleting the node is O(N).

        If we want to delete the first node, we just need to assign the next node to head.

  4. Design linked list

 

class MyLinkedList {
public:
    struct node{
        int val;
        node *next;
        node(int x): val(x),next(NULL){};
    };
    
    /** Initialize your data structure here. */
    MyLinkedList() {
        head = new node(0);
        size = 0;
    }
    
    /** 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>(size-1)) return -1;
        node *cur = head->next;    //The auxiliary pointer points to the first node
        while(index--) cur = cur->next;
        return cur->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) {
        node *cur = new node(val);
        cur->next = head->next;
        head->next = cur;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        node *cur = head;
        while(cur->next)
        {
            cur = cur->next;
        }
        node *newnode = new node(val);
        newnode->next = cur->next;    //Save the information of the last node at the end
        cur->next = newnode;          //Connect the new node after cur
        size++;
    }
    
    /** 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==0) addAtHead(val);
        else if(index == size) addAtTail(val);
        else if(index > size) return;
        else{
            node *pred = head;     //Yes, insert before indexth, and then head - > next
            node *ans = new node(val);
            //node *cur = new node(0);
            while(index>0)
            {
                pred = pred->next;
                index--;
            }
            ans->next = pred->next;
            pred->next = ans;
            size++;
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        if(index < 0 || index >=size)
        return;
        node *cur = head;
        while(index>0)
        {
            cur = cur->next;
            index--;
        }
        cur->next = cur->next->next;
        size--;
    }
private:
    int size;
    node *head;
};

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

 

 

Posted by Talon on Tue, 21 Sep 2021 23:41:50 -0700