简介
反转链表是面试中常考问题,题型变化就那几种,包括整个链表的反转、范围反转等,本篇文章将这些问题放在一起进行讨论和总结,并对多种解法进行分析和对比,做到“一网打尽”!
反转链表系列题目
以上题目均来自leetcode
例题详细
1、反转链表
题目描述
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
分析
虽然是一道简单题目,但是解法有很多种,还是有很多值得探讨的地方。
- 方法一:借用列表法
将链表的节点放在一个列表中,利用列表进行反转,最后将反转后的节点组成新的链表。
这种解法操作起来简单,因为我们熟悉的是列表的反转,而对于链表的反转有点无从下手。
时间复杂度:将节点放入列表O(n),反转列表O(n),组成新的链表O(n),所以是O(3n)≈O(n)。
空间复杂度:需要用到一个列表存储链表节点,O(n)。
这种解法时间空间都没有什么优势,因此放弃。
- 方法二:递归法
有链表:L1 -> L2 -> L3 -> L4,假如此时我们完成了L2到L4的反转,那么链表就是这样的:L4 -> L3 -> L2,同时L1 -> L2,此时我们需要将L1节点移动到L2之后,假设:
p: L4
head: L1
所以
head.next.next = head;
head.next = null;
描述半天可能说不清,直接看代码可能就能理解了。正所谓:talk is cheap, show me the code!
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
}
时间复杂度:需要对每个节点进行递归,因此时间复杂度为O(n)
空间复杂度:递归需要用到栈空间,每次的递归会在栈中压入一个栈帧,最大递归深度为O(n),但是相较于方法一中使用O(n)的堆空间,栈空间会伴随着递归结束而销毁,但是堆空间需要垃圾回收,所以栈空间稍稍有一点优势。
代码看起来清洁干净,且可读性强。
- 方法三 修改指针法
创建一个dummy节点,指向head,从前往后遍历节点,将next指针指向前一个节点。
例如:
Ld -> L1 -> L2 -> L3 -> L4
Ld <- L1 -> L2 -> L3 -> L4
Ld <- L1 <- L2 -> L3 -> L4
Ld <- L1 <- L2 <- L3 -> L4
Ld <- L1 <- L2 <- L3 <- L4
null <- L1 <- L2 <- L3 -> L4
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1), next, pre = dummy, cur = head;
dummy.next = head;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
head.next = null;
return pre;
}
}
这种方法理解起来比较容易,最后注意把head的next指针指向null,因为反转之后head就是尾节点了。
时间复杂度:O(n)
空间复杂度:O(1)
- 方法四 移动节点法
这种方法每次移动一个节点,从而达到反转的目的。
例如:
Ld -> L1 -> L2 -> L3 -> L4
Ld -> L2 -> L1 -> L3 -> L4
Ld -> L3 -> L2 -> L1 -> L4
Ld -> L4 -> L3 -> L2 -> L1
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = new ListNode(-1), cur = head, next;
pre.next = head;
while (cur != null && cur.next != null) {
next = cur.next;
cur.next = next.next;
next.next = pre.next;
pre.next = next;
}
return pre.next;
}
}
这种方法是接下来几种题型的基础。
时间复杂度:O(n)
空间复杂度:O(1)
2、反转链表 II
题目描述
给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
示例
输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]
分析
借用第1题的方法四,先找到第left个节点,然后再移动(right - left)次。
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if (left >= right) {
return head;
}
ListNode dummy = new ListNode(-1), pre = dummy;
dummy.next = head;
ListNode cur = head, next;
for (int i = 1; i < left; i++) {
pre = pre.next;
head = head.next;
}
for (int i = 0; i < right - left; i++) {
next = head.next;
head.next = next.next;
next.next = pre.next;
pre.next = next;
}
return dummy.next;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
3、K 个一组翻转链表
题目描述
给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
k 是一个正整数,它的值小于或等于链表的长度。
如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
进阶:
你可以设计一个只使用常数额外空间的算法来解决此问题吗?
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
示例
输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]
分析
每k个节点反转一次,不足k个时直接返回。
public class Solution {
private ListNode[] reverse(ListNode head, ListNode tail) {
ListNode dummy = new ListNode(-1), next;
dummy.next = head;
while (dummy.next != tail) {
next = head.next;
head.next = next.next;
next.next = dummy.next;
dummy.next = next;
}
return new ListNode[]{tail, head};
}
private ListNode[] reverse1(ListNode head, ListNode tail) {
ListNode pre = new ListNode(-1), next, tailNext = tail.next;
pre.next = head;
while (head != tailNext) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
head.next = null;
return new ListNode[]{tail, head};
}
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1), pre = dummy, tail, nxt;
dummy.next = head;
while (head != null) {
tail = pre;
for (int i = 0; i < k; i++) {
tail = tail.next;
if (tail == null) {
return dummy.next;
}
}
nxt = tail.next;
ListNode[] nodes = reverse1(head, tail);
pre.next = nodes[0];
nodes[1].next = nxt;
pre = nodes[1];
head = nxt;
}
return dummy.next;
}
其中 reverse 方法是移动节点法,reverse1 方法修改指针法。
时间复杂度:O(n)
空间复杂度:O(1)
本文深入探讨链表反转问题,涵盖单链表整体反转、部分反转及按K个元素分组反转等多种情况。提供了多种解法并分析其时间与空间复杂度。
292

被折叠的 条评论
为什么被折叠?



