灵感来源
- 保持更新,努力学习
- python脚本学习
反转链表
解题思路
-
迭代法:通过遍历链表,逐个改变节点的指针方向。具体步骤如下:
- 使用三个指针:
prev
(初始为None
)、curr
(初始为头节点)、next_node
(用于保存当前节点的下一个节点)。 - 在遍历过程中,先保存当前节点的下一个节点,然后将当前节点的指针指向前一个节点,最后更新
prev
和curr
指针。 - 重复上述步骤,直到遍历完整个链表,此时
prev
指针即为新的头节点。
- 使用三个指针:
-
递归法:通过递归调用反转后续节点,然后调整当前节点的指针方向。具体步骤如下:
- 递归反转当前节点的后续链表。
- 将当前节点的下一个节点的指针指向当前节点。
- 将当前节点的指针置为
None
,避免形成环。# Definition for singly-linked list. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: # 迭代方法 def reverseList(self, head: ListNode) -> ListNode: prev = None curr = head while curr: next_node = curr.next # 保存下一个节点 curr.next = prev # 反转指针 prev = curr # 移动prev指针 curr = next_node # 移动curr指针 return prev # 返回新的头节点 # 递归方法 def reverseListRecursive(self, head: ListNode) -> ListNode: if not head or not head.next: return head # 递归反转后续节点 new_head = self.reverseListRecursive(head.next) # 调整指针方向 head.next.next = head head.next = None return new_head # 辅助函数:将列表转换为链表 def list_to_linkedlist(lst): dummy = ListNode(0) current = dummy for val in lst: current.next = ListNode(val) current = current.next return dummy.next # 辅助函数:将链表转换为列表 def linkedlist_to_list(head): result = [] current = head while current: result.append(current.val) current = current.next return result # 示例用法 if __name__ == "__main__": # 创建链表 1->2->3->4->5 head = list_to_linkedlist([1, 2, 3, 4, 5]) # 使用迭代方法反转链表 solution = Solution() reversed_head = solution.reverseList(head) print("迭代方法反转后的链表:", linkedlist_to_list(reversed_head)) # 重新创建链表 1->2->3->4->5 head = list_to_linkedlist([1, 2, 3, 4, 5]) # 使用递归方法反转链表 reversed_head_recursive = solution.reverseListRecursive(head) print("递归方法反转后的链表:", linkedlist_to_list(reversed_head_recursive))
逐行解释
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val # 当前节点的值
self.next = next # 指向下一个节点的指针
class Solution:
# 迭代方法:通过遍历链表逐个反转指针方向
def reverseList(self, head: ListNode) -> ListNode:
prev = None # 初始化前一个节点为None
curr = head # 初始化当前节点为头节点
while curr: # 遍历链表直到当前节点为空
next_node = curr.next # 保存当前节点的下一个节点
curr.next = prev # 将当前节点的指针指向前一个节点
prev = curr # 前一个节点向后移动
curr = next_node # 当前节点向后移动
return prev # 返回新的头节点(原链表的尾节点)
# 递归方法:通过递归反转后续节点,再调整当前节点的指针
def reverseListRecursive(self, head: ListNode) -> ListNode:
if not head or not head.next: # 递归终止条件:节点为空或为尾节点
return head
# 递归反转后续节点,返回新的头节点
new_head = self.reverseListRecursive(head.next)
# 调整指针方向:将当前节点的下一个节点的next指向当前节点
head.next.next = head
# 断开当前节点的next指针,防止形成环
head.next = None
return new_head # 返回新的头节点
# 辅助函数:将列表转换为链表
def list_to_linkedlist(lst):
dummy = ListNode(0) # 创建虚拟头节点
current = dummy # 当前节点指向虚拟头节点
for val in lst: # 遍历列表
current.next = ListNode(val) # 创建新节点并连接
current = current.next # 当前节点后移
return dummy.next # 返回虚拟头节点的下一个节点,即真正的头节点
# 辅助函数:将链表转换为列表
def linkedlist_to_list(head):
result = [] # 初始化结果列表
current = head # 当前节点指向头节点
while current: # 遍历链表
result.append(current.val) # 将当前节点的值添加到列表
current = current.next # 当前节点后移
return result # 返回结果列表
# 示例用法
if __name__ == "__main__":
# 创建链表 1->2->3->4->5
head = list_to_linkedlist([1, 2, 3, 4, 5])
# 使用迭代方法反转链表
solution = Solution()
reversed_head = solution.reverseList(head)
print("迭代方法反转后的链表:", linkedlist_to_list(reversed_head))
# 重新创建链表 1->2->3->4->5
head = list_to_linkedlist([1, 2, 3, 4, 5])
# 使用递归方法反转链表
reversed_head_recursive = solution.reverseListRecursive(head)
print("递归方法反转后的链表:", linkedlist_to_list(reversed_head_recursive))