剑指 Offer 06. 从尾到头打印链表
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
方法一:辅助栈法
解题思路:
链表特点: 只能从前至后访问每个节点。
题目要求: 倒序输出节点值。
这种 先入后出 的需求可以借助 栈 来实现。
思路:
入栈: 遍历链表,使用 append() 方法将各节点值 push 入栈。
出栈: 将各节点值 pop 出栈,存储于数组并返回stack 的倒序列表。
复杂度分析:
时间复杂度 O(N): 入栈和出栈共使用 O(N) 时间。
空间复杂度 O(N): 辅助栈 stack 和数组 res 共使用 O(N) 的额外空间。
# python
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
stack = []
while head:
stack.append(head.val)
head = head.next
return stack[::-1]
方法二:递归法
利用递归: 先走至链表末端,回溯时依次将节点值加入列表 ,这样就可以实现链表值的倒序输出。
基本思路:
递推阶段: 每次传入 head.next ,以 head == None(即走过链表尾部节点)为递归终止条件,此时返回空列表 [] 。
回溯阶段: 利用 Python 语言特性,递归回溯时每次返回 当前 list + 当前节点值 [head.val] ,即可实现节点的倒序输出。
class Solution:
def reversePrint(self, head: ListNode) -> List[int]:
return self.reversePrint(head.next) + [head.val] if head else []
剑指 Offer 24. 反转链表
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输出: 5->4->3->2->1->NULL
限制:
0 <= 节点个数 <= 5000
方法一:迭代法
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur, pre = head, None
while cur:
tmp = cur.next # tmp指向cur.next
cur.next = pre # cur指向pre
pre = cur # pre 暂存 cur
cur = tmp # cur 访问下一节点
return pre
# 利用 Python 语言的平行赋值语法,可以进一步简化代码(但可读性下降):
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur, pre = head, None
while cur:
cur.next, pre, cur = pre, cur, cur.next
return pre
方法二:递归
调用并返回 recur(head, null) 。传入 null 是因为反转链表后, head 节点指向 null ;
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
def recur(cur, pre):
if not cur: return pre # 终止条件
res = recur(cur.next, cur) # 递归后继节点
cur.next = pre # 修改节点引用指向
return res # 返回反转链表的头节点
return recur(head, None) # 调用递归并返回
二者时间复杂度相同,迭代的空间复杂度为O(1),迭代法更优。
剑指 Offer 35. 复杂链表的复制
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
方法一:哈希表
利用哈希表的查询特点,考虑构建 原链表节点 和 新链表对应节点 的键值对映射关系,再遍历构建新链表各节点的 next 和 random 引用指向即可。
复杂度分析:
- 时间复杂度 O(N) : 两轮遍历链表,使用 O(N)时间。
- 空间复杂度 O(N) : 哈希表 dic 使用线性大小的额外空间。
基本思路:
-
若头节点 head 为空节点,直接返回 null ;
-
初始化: 哈希表 dic , 节点 cur 指向头节点;
-
复制链表:
-
建立新节点,并向 dic 添加键值对 (原 cur 节点, 新 cur 节点) ;
-
cur 遍历至原链表下一节点;
-
-
构建新链表的引用指向:
-
构建新节点的 next 和 random 引用指向;
-
cur 遍历至原链表下一节点;
-
-
返回值: 新链表的头节点 dic[cur] ;
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head: return
dic = {}
# 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
cur = head
while cur:
dic[cur] = Node(cur.val)
cur = cur.next
cur = head
# 4. 构建新节点的 next 和 random 指向
while cur:
dic[cur].next = dic.get(cur.next)
dic[cur].random = dic.get(cur.random)
cur = cur.next
# 5. 返回新链表的头节点
return dic[head]
方法二:拼接 + 拆分
考虑构建 原节点 1 -> 新节点 1 -> 原节点 2 -> 新节点 2 -> …… 的拼接链表,如此便可在访问原节点的 random 指向节点的同时找到新对应新节点的 random 指向节点。
-
设原链表为
n o d e 1 → n o d e 2 → ⋯ node1 \rightarrow node2 \rightarrow \cdots node1→node2→⋯
构建的拼接链表如下所示:
n o d e 1 → n o d e 1 n e w → n o d e 2 → n o d e 2 n e w → ⋯ node1 \rightarrow node1_{new} \rightarrow node2 \rightarrow node2_{new} \rightarrow \cdots node1→node1new→node2→node2new→⋯
构建新链表各节点的 random 指向:当访问原节点 cur 的随机指向节点 cur.random 时,对应新节点 cur.next 的随机指向节点为 cur.random.next 。
拆分原 / 新链表:设置 pre / cur 分别指向原 / 新链表头节点,遍历执行 pre.next = pre.next.next 和 cur.next = cur.next.next 将两链表拆分开。
返回新链表的头节点 res 即可。复杂度分析:
时间复杂度 O(N): 三轮遍历链表,使用 O(N) 时间。
空间复杂度 O(1): 节点引用变量使用常数大小的额外空间。class Solution: def copyRandomList(self, head: 'Node') -> 'Node': if not head: return cur = head # 1. 复制各节点,并构建拼接链表 while cur: tmp = Node(cur.val) tmp.next = cur.next cur.next = tmp cur = tmp.next # 2. 构建各新节点的 random 指向 cur = head while cur: if cur.random: cur.next.random = cur.random.next cur = cur.next.next # 3. 拆分两链表 cur = res = head.next pre = head while cur.next: pre.next = pre.next.next cur.next = cur.next.next pre = pre.next cur = cur.next pre.next = None # 单独处理原链表尾节点 return res # 返回新链表头节点