# 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

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.

# 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).
Output: [2,3,1]

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

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> vectorResult;
stack<ListNode*> nodes;
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
Example:

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

code:

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* cur=NULL;
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:

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getKthFromEnd(ListNode* head, int k) {
return NULL;
}
for(int i=0;i<k-1;i++){
}else{
return NULL;
}
}
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

```/**
* 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.

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