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

相关推荐
dtq042414 小时前
数据结构 - 堆
数据结构
nhdh21 小时前
B+树揭秘:MySQL索引核心原理全解析
数据结构·b树·mysql
蛋蛋的就会顺顺的1 天前
hot100——矩阵
java·数据结构·算法·leetcode·力扣
学代码的CJY1 天前
从代码出发理解时间复杂度与空间复杂度
数据结构·算法
字节跳动的猫1 天前
LikeShop 积分体系全链路改造:获取规则、订单抵扣与积分商品兑换二开
运维·数据结构
上位机妹子1 天前
C 语言 数组删除指定元素(快慢指针法)
c语言·数据结构·算法
想吃火锅10051 天前
【leetcode】238.除了自身以外数组的乘积js
javascript·算法·leetcode
C++ 老炮儿的技术栈1 天前
有符号变量与无符号变量的区别
服务器·开发语言·前端·数据结构·c++·算法·c
Tisfy1 天前
LeetCode 0020.有效的括号:栈匹配
算法·leetcode·字符串·题解·括号匹配
Navigator_Z1 天前
LeetCode //C - 1276. Number of Burgers with No Waste of Ingredients
c语言·算法·leetcode