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

相关推荐
郝学胜-神的一滴14 小时前
游戏设计、原型与开发 26:四种循环
开发语言·数据结构·游戏·游戏引擎·游戏策划
Thanks_ks15 小时前
Python 列表与元组:可变性与不可变性的差异及使用场景选择
数据结构·python·列表·元组·可变对象·不可变对象·可哈希
Shaoshing20 小时前
LeetCode 921.使括号括号有效的最少添加
java·算法·leetcode·职场和发展
圣保罗的大教堂1 天前
leetcode 3884. 双端字符匹配 简单
leetcode
@三十一Y1 天前
C++高阶数据结构:图
开发语言·数据结构·c++
夏日听雨眠1 天前
数据结构( Floyd)
数据结构·算法
金榜题名萱1 天前
蓝桥杯 - 妮妮的月饼工厂(二分)
c语言·数据结构·c++·算法·蓝桥杯·动态规划
小王C语言1 天前
优选算法:队列+宽选(bfs)
数据结构·算法·宽度优先
HQL_seven1 天前
【数据结构】线性结构
数据结构
龙砚序1 天前
【数据结构|二叉树实战 05-1】文件系统目录树构建与统计模块(C 语言递归实现)
数据结构