当前位置:网站首页>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
边栏推荐
- Go language installation and uninstallation
- Alicloud centos8.0 installing mysql8
- Is it safe to open a new bond securities account
- Applet Click to return to the top 2 methods
- PAT B1063
- 2.15(Multiple of 3 Or 5)
- Redis cache preheating & avalanche & breakdown & penetration
- Est - il sûr d'ouvrir un compte avec de nouvelles dettes? Une faible Commission est - elle crédible?
- <C>. Figure guessing game
- Redis high availability: do you call this the principle of master-slave architecture data synchronization?
猜你喜欢

PAT B1086

Applet wx Request encapsulation

Principles of MySQL clustered index and non clustered index

New features of php7

Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information

Vulnhub range - correlation:2

Jsonp processing non homologous

Redis practice: smart use of data types to achieve 100 million level data statistics

String since I can perform performance tuning, I can call an expert directly

Can GoogleSEO only do content without external chain? (e6zzseo)
随机推荐
How to understand var = a = b = C = 9? How to pre parse?
Determine whether it is a web page opened on wechat
通过启牛学堂开的股票账户可以用吗?资金安全吗?
VMware failed to prompt to lock this profile exclusively
PAT B1059
Error record: preg_ match(): Compilation failed: range out of order in character class at offset 13
Vulnhub range - the planes:venus
How do I delete an entity from symfony2
PAT B1066
Curtain down and departure
Now meditation: crash service and performance service help improve application quality
LNMP compilation and installation
System optimization method
Jsonp non homologous interaction (click trigger)
Mqtt+ardunio+esp8266 development (excluding mqtt server deployment)
Huawei HMS core launched a new member conversion & retention prediction model
打新债网上开户安全吗,需要注意什么
String since I can perform performance tuning, I can call an expert directly
PAT B1056
JQ implements tab switching