当前位置:网站首页>LeetCode刷题:反转链表 与 链表中的中间节点
LeetCode刷题:反转链表 与 链表中的中间节点
2022-07-21 14:25:00 【散一世繁华,颠半世琉璃】
1.反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示例一:
示例二:
示例三:
进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
1.双指针实现
分析题目可知,我们可以采用双指针来对链表进行反转。
我建议大家可以现在草稿纸上写下指针变化的过程,再开始编写程序,尤其是涉及链表的问题!
具体的步骤如下所示:
定义两个指针: pre和 cur;pre在前 cur在后。
每次均用next指针保存cur.next,然后让 cur.next指向 pre,以实现一次局部反转;
局部反转完成之后,pre和 cur同时往后移动一个位置
循环上述过程,直至 pre到达链表尾部结束!
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode reverseList(ListNode head) {
//双指针实现
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
//将cur的下一节点存储下来
ListNode next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
}
2.GIF动图展示
按照上述的操作步骤,在结合GIF动图演示,一定可以做到深刻的理解该算法的实现过程以及原理!
3.时间复杂度分析
由于本次算法的实现仍然是遍历一次链表,直到cur指向null结束,因此算法的时间复杂度为 O ( n ) O(n) O(n),空间复杂度为 O ( 1 ) O(1) O(1)。该算法在LeetCode中的执行情况如下所示:
2.链表中的中间节点
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
示例一:
示例二:
1.快慢指针
分析此问题,我们可以采用双指针方法解决该问题。
分别定义一个fast快指针,一次移动两位,定义一个slow慢指针,一次移动一位,那么当fast遍历到链表的末尾时,slow位置所指的节点就是中间节点!
具体代码实现如下所示:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;//慢指针移动一位
fast=fast.next.next;
}
return slow;
}
}
2.GIF动图演示
下面的GIF动图以链表{1,2,3,4,5,6,7,8}为例,采用快慢指针对其进行遍历,求解的具体过程如下所示:
3.时间复杂度分析
这里同样是对链表的一次遍历,时间复杂度为 O ( n ) O(n) O(n),由于只需要在常数空间存储两个指针即可,因此空间复杂度为 O ( 1 ) O(1) O(1);该算法在LeetCode中的执行情况如下所示:
边栏推荐
- What is the culprit of the failure of digital transformation?
- Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
- Development of digital collection system -- Construction of mall blind box H5 platform
- 开源进销存系统,10分钟搞定,建议收藏!
- Data center cable management
- 剑指 Offer II 100. 三角形中最小路径之和
- Postman - post request application / x-www-from-urlencoded
- 移动研发平台EMAS 3.0全新升级,欢迎登陆阿里云官网搜索EMAS进行体验
- leetcode 310. 最小高度树
- Encryption and decryption of yii2
猜你喜欢
leetcode 1306.跳跃游戏 III
iNFTnews | 佳士得推出风险投资部门,瞄准Web3和元宇宙产业
Development of digital collection system -- Construction of mall blind box H5 platform
Discussion on killing free exe Technology
Discussion on ASP webshell+ ice scorpion free horse killing
64. Minimum path and
Discussion on passing D shield JSP webshell+ ice scorpion avoiding killing horses
使用Lingo求解简单的线性规划问题
Analysis on the characteristics of two-layer industrial switch
Importance of data center operation and maintenance management skills
随机推荐
Kyligence 出席华为全球智慧金融峰会,加速拓展全球市场
The wonderful problem encountered in FPGA design - "chip also depends on origin" (III)
leetcode 1306. Jumping game III
Detr code
基于通用单片机(久齐) 半导体制冷制热的控制 手机散热器按摩仪器中冷热头的控制
Discussion on ASP webshell+ ice scorpion free horse killing
haproxy2.6负载安装配置
Random talk on DRDS flexible affairs
bypass 某狗sql和xss
leetcode1588.所有奇数长度子数组的和
leetcode 1582. 二进制矩阵中的特殊位置
Relevant use cases of QT events
92. (leaflet chapter) leaflet situation plotting - acquisition of attack direction
百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
C language problem solving number sequence
ssm框架文件上传已经实现怎么映射到数据库
[MATLAB]:基础知识学习
Encryption and decryption of yii2
streamlit TypeError: Plain typing. NoReturn is not valid as type argument
MySQL: how are MySQL clients and servers connected?