当前位置:网站首页>*List reversal
*List reversal
2022-07-27 16:25:00 【Cute rain】
Title Description : Define a function , Enter the head node of a linked list , Invert the linked list and output the head node of the inverted linked list
// Head node definition
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};Ideas : For linked list inversion , The most common method is to insert and reverse the nodes all the time next 了 , These two methods have two cases respectively , The linked list has head nodes and the linked list has no head nodes . Straight in : Straight head insertion is to remove the nodes of the original linked list from front to back, and then reconnect to the head node , Because the first inserted node is always behind the linked list , So after re inserting the head , The linked list is inverted .
Method 1 : Always insert the diagram ( Headless node ):

Code implementation
ListNode* reverseList(ListNode* head) {
ListNode* p = head;
ListNode* s = NULL;
head = NULL;
while (p != NULL)
{
s = p;
p = p->next;
s->next = head;
head = s;
}
return head;
}There are head nodes :

Code implementation :
ListNode* Reverse(ListNode* p)
{
assert(p != NULL);
if (p == NULL && p->next == NULL)
{
return p;
}
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->next = NULL;
while (p != NULL)
{
ListNode* s = p;
p = p->next;
s->next = head->next;
head->next = s;
}
p = head->next;
free(head);
return p;
}
Method 2 : Reverse the list of next Domain diagram ( Headless node ):

Code implementation :
ListNode* reversePrint(ListNode* head)
{
ListNode *p=NULL;
ListNode *q=head->next;
ListNode *r=head->next;
head->next=NULL;
while(r->next!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
return head;
}Header node code implementation :
ListNode* reversePrint(ListNode* head)
{
ListNode *p=NULL;
ListNode *q=head->next->next;
ListNode *r=head->next->next;
head->next->next=NULL;
head->next=NULL;
while(r->next!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
head->next=q;
return head->next;
}Method 3 : Recursive inversion :
ListNode* RevList(ListNode* pre, ListNode* p)
{
ListNode* tail = NULL;
if (p != NULL)
{
tail = RevList(p, p->next);
p->next = pre;
}
else
{
tail = pre;
}
return tail;
}
边栏推荐
- JSP Foundation
- Time series - use tsfresh for classification tasks
- DRF use: get request to get data (small example)
- EXE程序加密锁
- : 0xc0000005: an access conflict occurs when writing position 0x01458000 - to be solved
- The difference and use between get request and post request
- centos yum方式安装mysql
- Cubemx联合IAR工程移植
- C语言逆序输出字符串
- Pychart import existing project
猜你喜欢

Your password does not satisfy the current policy requirements (modify MySQL password policy setting simple password)

centos上mysql5.7主从热备设置

Wechat applet personal number opens traffic master

201403-1

DEX and AMMS of DFI security

The whereor method of TP5 has many conditions

第31回---第52回

MapReduce instance (II): Average

Time series ARIMA model

my_ls小结
随机推荐
Boolean value
Cron expression use
TP5 paging some small points
云管平台中租户以及多租户概念简单说明
JMeter5.3 及以后的版本jmeter函数助手生成的字符在置灰无法复制
These questions~~
2021-06-02
TP5 -- query field contains a certain --find of search criteria_ IN_ SET
The solution to the memory exhaustion problem when PHP circulates a large amount of data
mysql设置密码时报错 Your password does not satisfy the current policy requirements(修改·mysql密码策略设置简单密码)
Two methods of generating excel table with PHP
Rare bitwise operators
Chuanyin holdings disclosed that it was prosecuted by Huawei: a case has been filed, involving an amount of 20million yuan
Pointer summary
Leetcode 226 翻转二叉树(递归)
Addition of large numbers
Samsung closes its last mobile phone factory in China
Mapreduce实例(三):数据去重
嵌入式面试
The difference and use between get request and post request