当前位置:网站首页>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
边栏推荐
- Mqtt+ardunio+esp8266 development (excluding mqtt server deployment)
- New features of redis 6.0: take you 100% to master the multithreading model
- PAT B1056
- <C>. String comparison
- 2、 Hikaricp source code analysis of connection acquisition process II
- Does redis transaction support acid?
- Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
- 三、HikariCP获取连接流程源码分析三
- <C>. Calculation date to day conversion
- ActiveMQ--CVE-2016-3088
猜你喜欢

Using flex to implement the Holy Grail layout is as simple as that

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

<C>. function

rmi-registry-bind-deserialization

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

Arduino read temperature

New features of php7

Bindgetuserinfo will not pop up

My official account writing experience sharing

<C>. tic-tac-toe
随机推荐
通过启牛学堂开的股票账户可以用吗?资金安全吗?
H5 application conversion fast application
JS advanced
Jsonp function encapsulation
Vulnhub range the planes:earth
PostgreSQL change table owner
PAT B1059
打新债网上开户安全吗,需要注意什么
C language PTA -- continuity factor
Mqtt+ardunio+esp8266 development (excluding mqtt server deployment)
SEO outsourcing reliable company, enterprise SEO outsourcing company which reliable?
Google SEO external chain releases 50+ website platform sharing (e6zzseo)
Wechat applet swiper simple local picture display appears large blank
Read multiple associations from a field using delimiters in laravel
PostgreSQL division considerations
Wechat applet connects to the server to display mqtt data information
6、 Configuration resolution of hikariconfig
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
Vulnhub range - darkhole 1
Does GoogleSEO need to change the friend chain? (e6zzseo)