当前位置:网站首页>206. reverse linked list (insert, iteration and recursion)
206. reverse linked list (insert, iteration and recursion)
2022-06-25 20:02:00 【The ninth tree】
Create a new chain header insertion method
This method requires creating an additional linked list , Then use the head plug , In turn head The new node is inserted into the new node , Implement inversion .
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode *newHead=new ListNode(0);
while(head!=NULL)
{
ListNode *p=head->next;
head->next=newHead->next;
newHead->next=head;
head=p;
}
return newHead->next;
}
};
Detailed explanation :
Iterative double pointer
This algorithm requires setting two node pointers , One before and one after , Transfer the points of nodes in the linked list , Until all nodes of the pointer are traversed .
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode *pre=NULL;
ListNode *cur=head;
while(cur!=NULL)
{
ListNode *p=cur->next;
cur->next=pre;
pre=cur;
cur=p;
}
return pre;
}
};
Iteration double pointer error version
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode *pre=head;// If the last node of the linked list is flipped in this way, it will not point to NULL
ListNode *cur=head->next;// Empathy
while(pre!=NULL)// The termination condition here is wrong , Should be cur!=NULL
{
ListNode *p=cur;
cur->next=pre;
pre=p;
cur=p->next;
}
return pre;
}
};
Detailed explanation :
recursive
Three conditions of recursion :
1、 It can be broken down into sub problems
2、 The sub problem is solved in the same way as the original problem
3、 There is a condition for terminating recursion , The smallest question
It can be understood as a sub problem , That is, reverse the whole composed of the head node and other nodes except the head node , Then it is equivalent to reversing a linked list containing two nodes .
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode *p=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return p;
}
};
This picture is quoted from here
边栏推荐
- JS asynchronism (I. asynchronous concept, basic use of web worker)
- Jsonp function encapsulation
- How to understand var = a = b = C = 9? How to pre parse?
- Applet multi image to Base64 upload
- JS advanced
- Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
- Database data type design (the most detailed in the whole network)
- Appearance of object attributes
- <C>. Calculation date to day conversion
- 通过启牛学堂开的股票账户可以用吗?资金安全吗?
猜你喜欢
<C>. Branch and loop statements
Vulnhub range - the planes:venus
String since I can perform performance tuning, I can call an expert directly
Mail monitoring cloud script execution progress
Process of vacuum and vacuum full
Web components - Basics
Uni app through uni Navigateto failed to pass parameter (pass object)
Impact of Huawei application transfer and application claim on user identification
New features of redis 6.0: take you 100% to master the multithreading model
Automatic fitting when the applet reaches the top
随机推荐
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
Web container basic configuration
Wechat applet connects to the server to display mqtt data information
System optimization method
Panda weekly -2022/02/18
<C>. Figure guessing game
1、 Hikaricp source code analysis of connection acquisition process I
Vulnhub range - darkhole 1
Yaml configuration
RPM package installation command
Alicloud centos8.0 installing mysql8
Read multiple associations from a field using delimiters in laravel
Redis high availability: do you call this the principle of master-slave architecture data synchronization?
VMware failed to prompt to lock this profile exclusively
Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information
Web components - Basics
Convert word to PDF through libreoffice
Analyse du code source du processus d'acquisition et de connexion hikaricp II
Laravel validation rule followed Role of auth:: id()
<C>. array