当前位置:网站首页>*链表逆转
*链表逆转
2022-07-15 16:48:00 【幽萌之雨】
题目描述:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点
//头结点定义
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};思路:对于链表逆置,最常用的方法就是一直头插了和反转节点的next了,这两种方法又分别有两种情况,链表有头结点和链表无头结点。一直头插:一直头插就是将原有链表的节点从前到后一个一个拆下来再重新连接到到头结点上,因为先插的节点总是在链表后边,所以重新头插完之后,链表就形成逆置了。
方法一:一直头插图解(无头结点):

代码实现
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;
}有头结点:

代码实现:
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;
}
方法二:反转链表的next域图解(无头结点):

代码实现:
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;
}有头结点代码实现:
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;
}方法三:递归逆置:
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;
}
边栏推荐
- The IP address of the database is stored. What data type is used
- FFmpeg sample 分析:muxing.c
- 基于TaskScheduler和CronTask实现动态增删启停定时任务功能
- Markdown learning notes Chapter 2 basic grammar (displayed in non markdown editor)
- 免驱 USB 转串口 Billboard 芯片(LDR2001)
- 示波器使用概念记录
- [interview brush 101] double pointer
- Flex & bison advanced calculator
- tensorflow图像数据增强预处理
- A rate distortion optimization of h264 encoder (1)
猜你喜欢
![[cloud native] 3.4 ruoyi cloud deployment practice (Part 1)](/img/42/bd8ab0b5e51e181b71b9028bd363ad.png)
[cloud native] 3.4 ruoyi cloud deployment practice (Part 1)

免驱 USB 转串口 Billboard 芯片(LDR2001)

FFmpeg sample 分析:muxing.c

DEBUG系统

Live broadcast artifact Lavalier wireless microphone-ldr6028 power charging OTG scheme

Compileflow Taobao Workflow Engine

初学者怎么快速学会SQL

Exploration of yolact model structure

Markdown learning notes Chapter 2 basic grammar (displayed under the markdown editor)

1-first knowledge of FPGA
随机推荐
DEBUG系统
Chapter 10 pointer of C language
The meaning of sprint in Agile Development
Markdown learning notes Chapter 2 basic grammar (displayed in non markdown editor)
2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(9)思路
scala 分支控制 (单分支、双分支、多分支)、 分支判断的返回值
What is base?
PD QC誘騙取電應用IC《樂得瑞LDR6328S》廣泛應用於各大小家電
Tips for setting dropout parameters
C语言 第七章 预处理
60岁开发者的建议,尝试改变一下吧!
【面试必刷101】哈希
深度学习-损失函数
tensorflow图像数据增强预处理
Linked list implementation of C language stack
枚举,你了解它吗?
非对称加密RSA与对称加密AES项目应用
1-初识FPGA
9. MySQL -- JDBC入门
Reading notes: review of process consulting I II III