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
相关推荐
m0_5648768419 分钟前
提示词工程手册学习
人工智能·python·深度学习·学习
米粒133 分钟前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
波诺波37 分钟前
p1项目system_model.py代码
开发语言·python
静心观复1 小时前
Python 虚拟环境与 pipx 详解
开发语言·python
卷心菜狗1 小时前
Re.从零开始使用Python构建本地大模型网页智慧聊天机器人
开发语言·python·机器人
AlenTech1 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣1 小时前
力扣热门100题之合并两个有序链表
算法·leetcode·链表
书到用时方恨少!1 小时前
Python NumPy 使用指南:科学计算的基石
开发语言·python·numpy
8Qi81 小时前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
L-李俊漩2 小时前
荆华密算 面试题(大模型开发)
python