当前位置:网站首页>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
边栏推荐
- Redis high availability: do you call this the principle of master-slave architecture data synchronization?
- 2.15(Multiple of 3 Or 5)
- Applet password input box
- JS asynchronism (I. asynchronous concept, basic use of web worker)
- Pta--7-20 exchange minimum and maximum (15 points)
- 一、HikariCP获取连接流程源码分析一
- Mqtt+ardunio+esp8266 development (excluding mqtt server deployment)
- PAT B1063
- PostgreSQL division considerations
- <C>. Rolling phase division
猜你喜欢

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

What are Baidu collection skills? 2022 Baidu article collection skills

<C>. tic-tac-toe

How to understand var = a = b = C = 9? How to pre parse?

What should I pay attention to in GoogleSEO content station optimization?

Web container basic configuration

Can GoogleSEO only do content without external chain? (e6zzseo)

New features of redis 6.0: take you 100% to master the multithreading model

DARKHOLE 2

Redis core article: the secret that can only be broken quickly
随机推荐
C language PTA -- continuity factor
Please do not call Page constructor in files
二、HikariCP获取连接流程源码分析二
System optimization method
<C>. function
What should I pay attention to in GoogleSEO content station optimization?
PAT B1053
Dependency injection in PHP reflection implementation framework
wooyun-2014-065513
Log in to Huawei game with a minor account, and pop up anti addiction prompt after startup
Does redis transaction support acid?
<C>. array
mysql load data infile
Mqtt+ardunio+esp8266 development (excluding mqtt server deployment)
Applet multi image to Base64 upload
New features of php7
Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information
Redis practice: smart use of data types to achieve 100 million level data statistics
Randomly generate 100 non repeating numbers between 1 and 150 and put them in the array
Convert word to PDF through libreoffice