206. 反转链表 - 力扣(LeetCode)

图示

代码

python 复制代码
# encoding = utf-8
# 开发者:Alen
# 开发时间: 16:06 
# "Stay hungry,stay foolish."

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        def reverseList(node):
            prev = None
            cur = head
            while cur:
                next_temp = cur.next # 保存下一个节点
                cur.next = prev # 指针反向
                prev = cur # prev前移
                cur = next_temp # cur前移
            return prev
        return reverseList(head)

结果

解题步骤:www.bilibili.com

相关推荐
踩坑记录1 天前
leetcode hot100 easy 101. 对称二叉树 递归 层序遍历 bfs
算法·leetcode·宽度优先
老鼠只爱大米1 天前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
智码未来学堂1 天前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
青桔柠薯片1 天前
数据结构:顺序表与链表
数据结构·链表
金枪不摆鳍1 天前
算法--二叉搜索树
数据结构·c++·算法
向哆哆1 天前
画栈 · 跨端画师接稿平台:基于 Flutter × OpenHarmony 的整体设计与数据结构解析
数据结构·flutter·开源·鸿蒙·openharmony·开源鸿蒙
季明洵1 天前
C语言实现顺序表
数据结构·算法·c·顺序表
圣保罗的大教堂2 天前
leetcode 3650. 边反转的最小路径总成本 中等
leetcode
历程里程碑2 天前
Linxu14 进程一
linux·c语言·开发语言·数据结构·c++·笔记·算法
木井巳2 天前
【递归算法】验证二叉搜索树
java·算法·leetcode·深度优先·剪枝