当前位置:网站首页>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
边栏推荐
- The functions in the applet page are better than those in app JS first execution solution
- Yum command
- Can GoogleSEO only do content without external chain? (e6zzseo)
- 2.2 step tariff
- <C>. String comparison
- Redis is a loser. If you don't understand the usage specification, you will spoil it
- PHP FPM, workman, spoole, golang simple performance test
- Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
- PAT B1051
- ActiveMQ--CVE-2016-3088
猜你喜欢

Panda weekly -2022/02/18

Google SEO external chain releases 50+ website platform sharing (e6zzseo)

Number of wechat applet custom input boxes

Does GoogleSEO need to change the friend chain? (e6zzseo)

wooyun-2014-065513

Automatic fitting when the applet reaches the top

Pta--7-20 exchange minimum and maximum (15 points)

<C>. Figure guessing game

Does redis transaction support acid?

PHP synchronizes website content to hundreds of websites to improve SEO ranking
随机推荐
Number of wechat applet custom input boxes
Web components - Basics
PAT B1061
Two types of attribute injection methods
Force wechat page font size to be 100%
PAT B1067
Vulnhub range the planes:earth
rmi-registry-bind-deserialization
Database data type design (the most detailed in the whole network)
Teach you how to add custom controls to a map
5、 Initialization analysis II of hikaricp source code analysis
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
打新债证券开户安全吗
Does GoogleSEO need to change the friend chain? (e6zzseo)
CG kit explore high performance rendering on mobile terminal
String since I can perform performance tuning, I can call an expert directly
PHP little knowledge record
Thymleaf template configuration analysis
PAT B1066
mysql load data infile