Python | Leetcode Python题解之第206题反转链表

题目:

题解:

python 复制代码
# 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]:
        newHead = ListNode(0)
        newHead.next = None
        h = head
        while h:
            cur = ListNode(h.val)
            cur.next = newHead.next
            newHead.next = cur
            h = h.next
        
        return newHead.next
相关推荐
老胖闲聊5 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1185 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之5 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
lyaihao6 小时前
使用python实现奔跑的线条效果
python·绘图
ai大师7 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
小小爬虾7 小时前
关于datetime获取时间的问题
python
_Itachi__7 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
蓝婷儿8 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
chao_7899 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
大霞上仙9 小时前
nonlocal 与global关键字
开发语言·python