转载请注明出处:https://blog.csdn.net/loiter2/article/details/108185055
【剑指offer刷题】JZ15:反转链表
该文章只是用来记录一下刷题过程,本文的记录和解答用的Java语言。在刷到这道题之前,在帅地的《程序员内功修炼》中看到过对应的题,对刷这道题启发很大,很棒的一个博主,推荐关注其公众号:帅地玩编程。
1、题目分析
1.1 题目描述与理解
输入一个链表,反转链表后,输出新链表的表头。
1.2 解法分析
- 记录链表数据
对内存要求不高时,可以直接选择将链表的每个结点记录下来,再重新构建这个链表,从而达到反转链表的目的。因为要反转,而栈先入后出,刚好贴合要求。
时间复杂度:O(n),遍历链表并入栈,出栈并重新构建链表;
空间复杂度:O(n); - 递归
是看帅地的《程序员内功修炼》时学到的方法,是通过不断递归来遍历链表,并在递归过程中,修改每个结点的指向。
时间复杂度:O(n),遍历一遍链表;
空间复杂度:递归栈的空间; - 三指针方法
如下图所示,定义3个指针pre、cur、nex
pre指向已经反转后部分的最后一个结点;
cur指向未反转部分的第一个结点,接下来就要旋转它;
nex指向未反转部分的第二个结点,用于保存未反转部分链表,避免丢失
通过如下代码,可以一步步进行反转,反转示意图如下图所示:
nex = cur.next;
cur.next = pre;
pre = cur;
cur = nex;
时间复杂度为:O(n);
空间复杂度为:O(1);
下面分别给出三种方法的代码实现。
2、代码实现
作为学习,最好看了上面的思路后,自己去实现代码,学习效果更好。
2.1 记录链表数据
public class Solution {
if (head == null) return null;
Stack<ListNode> temp = new Stack<>();
while (head != null){ //将链表结点全部压入栈
temp.push(head);
head = head.next;
}
ListNode newHead = null;
if (!temp.empty()) newHead = temp.pop();
ListNode cur = newHead;
while (!temp.empty()){
cur.next = temp.pop();
cur = cur.next;
}
cur.next = null; //这一句是必要的,因为原来的头节点的next是指向第二个结点的,
//若不将其复制为null,得到的反转链表最后部分将是两个结点的不断循环
return newHead;
}
2.2 递归
public class Solution {
if (head == null) return null;
if (head.next == null) return head;
ListNode temp = head.next;
ListNode newHead = ReverseList(head.next);
temp.next = head;
head.next = null; //重点!!!!!
//这一句是必要的,否则链表的建立是混乱的
return newHead;
}
2.3 三指针
public class Solution {
ListNode cur = head;
ListNode pre = null, nex;
while (cur != null){
nex = cur.next;
cur.next = pre;
pre = cur;
cur = nex;
}
return pre;
}