【链表】No. 0206 反转链表 【简单】👉力扣对应题目指路
 
 
希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
欢迎关注、订阅专栏 【力扣详解】谢谢你的支持!
⭐ 题目描述:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
- 示例:
 
 
🔥 思路:必须要掌握的反转链表:反转
next指向即可
参考如上思路,给出详细步骤如下:
- 步骤一⭐定义
precurtemp分别指向当前节点的上一节点、当前节点、当前节点的下一节点
- 初始化
pre为虚拟头节点None- 初始化
cur为头节点current_head- 步骤二⭐针对当前要处理的节点
cur:改变其指向 → 为 ←
- 修改
cur.next为pre- 步骤三⭐
precurtemp一步一步向后移动,逐个节点处理
# 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: Optional[ListNode]) -> Optional[ListNode]:
        def reverse(current_head):
        	# --------------------------------------------------- step 1
            pre = None          ## 当前节点的上一节点  # -------- step 1.1
            cur = current_head  ## 当前节点  # ----------------- step 1.2
            
            while cur:   
            	## 改连当前节点指向原下一节点:为当前节点指向原上一节点 
                temp = cur.next ## 当前节点的下一节点
                cur.next = pre  ## 改连操作  # ------------------ step 2
                ## 准备处理下一处  # ----------------------------- step 3
                pre = cur
                cur = temp
            
            return pre
        
        return reverse(head)
希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
🔥 LeetCode 热题 HOT 100



















