当前位置:网站首页>leetcode一步解决链表反转问题
leetcode一步解决链表反转问题
2022-08-02 05:08:00 【你食不食油饼】
题目描述:


看到链表反转,小伙伴们心中不禁会想,这种题我做过无数次了, 太简单了,但是大家有没有把解法研究透彻呢?今天我就来给大家讲讲我理解的几种链表反转方法(我理解的哈哈哈)
注:大家学习的时候可以跟着画图过一遍,方便理解
1、利用栈
思路:利用栈就是一个很普遍的方法了,就是利用栈先入后出的特性,完美符合链表反转所需要的条件,在这里我们直接上代码:
public ListNode ReverseList(ListNode head) {
Stack<ListNode> stack = new Stack<>();
ListNode temp = head;
//1、入栈
while (temp != null){
stack.push(temp);
temp = temp.next;
}
ListNode result = stack.pop();
ListNode listNode = result;
//2、出栈
while (stack.size() != 0){
ListNode pop = stack.pop();
result.next = pop;
result = result.next;
}
result.next = null;
return listNode;
}时间复杂度:O(n)
空间复杂度:O(n)
2、从头节点开始连接
思路:我们直接采用从头到尾,依次连接到新的辅助节点后面,最后再返回辅助接点.next
这个时候我们有两种情况:
1)、一种是固定好头节点,不断插入到辅助接点后面


如图,不断插入进去,直到最后一个节点也插入到辅助节点后面
很好理解,我们来看看代码:
public ListNode reverseList(ListNode head){
ListNode pre = null;
ListNode temp = head;
ListNode next;
while (temp != null) {
next = temp.next;
temp.next = pre;
pre = temp;
temp = next;
}
return result;
}
2)、先定义一个尾部节点

把原来链表的头节点不断连接到新的链表上,代码如下:
public ListNode reverseList3(ListNode head){
if(head == null || head.next == null) return head;
ListNode result = null;
ListNode cur = head;
ListNode next;
while (cur != null){
next = cur.next;
cur.next = result;
result = cur;
cur = next;
}
return result;
}时间复杂度:O(n)
空间复杂度:O(1)
3、在原链表中进行反转
思路:既然我们可以把元素放到新的头结点上, 那我们一定会想在原来的链表上能不能进行反转呢?答案是可以,我们不需要一次反转成功,一次一次来,给大家画个图方便理解

这是第一次反转,每一步反转一次,直到反转完成 ,我们看看代码:
public ListNode reverseList2(ListNode head){
if(head == null || head.next == null) return head;
ListNode pre = new ListNode(-1);
pre.next = head; //前序节点
ListNode cur = head;
int len = 0; //链表总长度
ListNode temp = head;
while (temp != null){
len++;
temp = temp.next;
}
for (int j = 1; j < len; j++) {
temp = cur.next;
cur.next = temp.next;
temp.next = pre.next;
pre.next = temp;
}
return pre.next;
}时间复杂度:O(n)
空间复杂度:O(1)
总结:链表反转虽然是一个十分老套的题目,但是绝对值得大家学习透彻,学会这几种方法,大家保证以后遇到有关链表反转的题目,直接拿捏~
边栏推荐
- Go语言中定时任务库Cron使用详解
- MySQL implements sorting according to custom (specified order)
- Redis数据库
- Google Chrome(谷歌浏览器)安装使用
- Go language study notes - grpc serverclient protobuf Go language from scratch
- Stress testing and performance analysis of node projects
- el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
- navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
- 21天学习挑战赛安排
- 100 latest software testing interview questions in 2022, summary of common interview questions and answers
猜你喜欢

Detailed explanation of AMQP protocol

本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
![[PSQL] window function, GROUPING operator](/img/95/5c9dc06539330db907d22f84544370.png)
[PSQL] window function, GROUPING operator

leetcode每天5题-Day04

面试官:设计“抖音”直播功能测试用例吧

软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?

ELK log analysis system

100 latest software testing interview questions in 2022, summary of common interview questions and answers

面试测试工程师一般会问什么?测试主管告诉你

Google 安装印象笔记剪藏插件
随机推荐
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
MySQL String Concatenation - Various String Concatenation Practical Cases
How Navicat Connects to MySQL
Introduction and use of apifox (1).
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
卸载redis
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
LeetCode brush topic series - 787 K station transfer within the cheapest flight
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
MySQL implements sorting according to custom (specified order)
C language entry combat (13): decimal number to binary
面试测试工程师一般会问什么?测试主管告诉你
eggjs controller层调用controller层解决方案
本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
leetcode 665. Non-decreasing Array 非递减数列(中等)
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
Detailed explanation of interface in Go language
Matlab论文插图绘制模板第41期—气泡图(bubblechart)
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
165.比较版本号