当前位置:网站首页>leetcode 206.反转链表
leetcode 206.反转链表
2022-04-21 06:37:00 【小学五年级在读的蒟蒻】
leetcode 206.反转链表
大家好,我是小学五年级在读的蒟蒻,专注于后端,一起见证蒟蒻的成长,您的评论与赞与关注是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。
题目描述
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
题解一
使用双指针和头插法求解
思路:开辟一个新空间来使用头插法将原链表的数据不断插入新开辟的链表中
/* * @lc app=leetcode.cn id=206 lang=cpp * * [206] 反转链表 */
// @lc code=start
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution
{
public:
ListNode *reverseList(ListNode *head)
{
ListNode *reHead = NULL;
ListNode *cur = head;
ListNode *tmp;
while (cur)
{
tmp = cur->next;
cur->next = reHead;
reHead = cur;
cur = tmp;
}
return reHead;
}
};
// @lc code=end
题解二
使用递归反转链表
思路:主要还是遍历节点,在遍历每个节点的时候就开始反转链表,这样递归下去,到最后的时候就可以完成链表的反转了
/* * @lc app=leetcode.cn id=206 lang=cpp * * [206] 反转链表 */
// @lc code=start
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution
{
public:
ListNode *reverse(ListNode *cur, ListNode *pre)
{
//遍历结束
if (pre == nullptr)
{
return cur;
}
//每一步都反转链表
ListNode *tmp = pre->next;
pre->next = cur;
cur = pre;
pre = tmp;
//进入递归
return reverse(cur, pre);
}
ListNode *reverseList(ListNode *head)
{
//将反转后的链表尾部null值传入
ListNode *cur = nullptr;
ListNode *pre = head;
return reverse(cur, pre);
}
};
// @lc code=end
版权声明
本文为[小学五年级在读的蒟蒻]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44229867/article/details/122418925
边栏推荐
猜你喜欢

解释器模式(3.7-3.13)

NP、OSPF路由聚合

Cs5518, Mipi to dual LVDS, replacement: gm8775 of Guoteng, tc358775 of Toshiba, dual LVDS, domestic perfect replacement, DSI to dual channel LVDS, LVDS clock frequency up to 154mhz, 1920 x1200

2022-1-3至2022-1-16 访问者模式

Configure MySQL remote access

Knowledge of classes and objects

Domestic Poe, rpc304, replace ip802, ip804 and ip808, replace tps23861, ltc4292, pd69204 and si3459, start domestic Poe power supply and PSE, and ruipukang domestic Ethernet power supply and interface

Sort method (2) = > insert sort

2022.2.14-2.27 责任链模式

【WPF】级联Combobox
随机推荐
树状数组
BGP 自动路由聚合
Nmap scanning and scapy projects
NP、OSPF监测调试
NP、OSPF邻居邻接关系
【WPF】自定义控件
mysql底层的引擎选择,为什么索引结构选择B+树:(3.28-4.3)
China Resources micro power amplifier cs3850eo, 2 × 40W class D audio power amplifier circuit, replacement: Zhipu core cs8673, tas5780 and tas5754 of Ti, domestic power amplifier
BGP neighbor
论文阅读:Domain Name Encryption Is Not Enough: Privacy Leakage via IP-based Website Fingerprinting
论文阅读:Supporting Early and Scalable Discovery of Disinformation Websites
Ruiyuan ry8132 and ry9140 DCDC are mainly replaced by tps563200 and tps563201 of Ti, mp1471 and mp1653 of Xinyuan, and details of sy8104 power chip of silijie
策略模式(2.28-3.6)
Login interface universal password bypass
关于平衡二叉树
Solve the problem of Chinese garbled code
NP、OSPF 缺省路由
Cs5801 specification | cs5801 HDMI to EDP conversion scheme | HDMI to DP adapter board design, hdmi2 0 turn edp1 4. Support downward compatibility
Macro crystal micro ms9288, analog RGB (VGA) to HDMI, HDMI transmitter, audio decoder, built-in MCU
数据库死锁总结:(3.7-3.13)