C + + linked list related algorithm problems

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

preface

Tip: Here you can add the general contents to be recorded in this article:
For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Print linked list from end to end & reverse linked list

Title Description: enter the head node of a linked list, and return the value of each node from tail to head (returned by array).
Input: head = [1,3,2]
Output: [2,3,1]

Idea: using FILO characteristics of stack to realize
The code is as follows (example):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        vector<int> vectorResult;
        stack<ListNode*> nodes;
        ListNode* pNode=head;//Protective effect
        while(pNode!=NULL){
            nodes.push(pNode);
            pNode=pNode->next;
        }
        while(!nodes.empty()){
            pNode=nodes.top();
            vectorResult.push_back(pNode->val);
            nodes.pop();
        }
        return vectorResult;
    }
};

Sword finger Offer 24. Reverse linked list
Define a function, input the head node of a linked list, invert the linked list and output the head node of the inverted linked list.
Example:

Input: 1 - > 2 - > 3 - > 4 - > 5 - > null
Output: 5 - > 4 - > 3 - > 2 - > 1 - > null

code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur=NULL;
        ListNode* pre=head;
        while(pre!=NULL){
            ListNode* t=pre->next;
            pre->next=cur;
            cur=pre;
            pre=t;
        }
        return cur;
    }
};

2, Penultimate node

Title:
Input a linked list and output the penultimate node in the linked list. In order to conform to the habit of most people, this question starts from 1, that is, the tail node of the linked list is the penultimate node.

For example, a linked list has six nodes. Starting from the beginning, their values are 1, 2, 3, 4, 5 and 6. The penultimate node of the linked list is a node with a value of 4.

Knowledge points involved: fast and slow pointer method

Advantage: you can avoid traversing twice
The code is as follows:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        if(head==NULL || k==0){
            return NULL;
        }
        ListNode* pAHead=head;
        ListNode* pBehind=head;
        for(int i=0;i<k-1;i++){
            if(pAHead->next!=NULL){
                pAHead=pAHead->next;
            }else{
                return NULL;
            }
        }
        while(pAHead->next){
            pAHead=pAHead->next;
            pBehind=pBehind->next;
        }
        return pBehind;
    }
};

3, Merge two sorted linked lists

Enter two incrementally sorted linked lists, merge the two linked lists and make the nodes in the new linked list still incrementally sorted.

Example 1:

Input: 1 - > 2 - > 4, 1 - > 3 - > 4
Output: 1 - > 1 - > 2 - > 3 - > 4 - > 4

Knowledge points involved: linked list, recursion

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1==NULL){
            return l2;
        }else if (l2==NULL){
            return l1;
        }

        ListNode* mergeList=NULL;
        if (l1->val<l2->val){
            mergeList=l1;
            mergeList->next=mergeTwoLists(l1->next,l2);
        }else{
            mergeList=l2;
            mergeList->next=mergeTwoLists(l1,
            l2->next);
        }
        return mergeList;
    }
};

summary

The above are the linked list algorithm problems they are currently exposed to, and will be expanded in the future.

Tags: C++ Algorithm linked list

Posted on Sat, 04 Dec 2021 17:20:49 -0500 by psycho_somatic