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

相关推荐
琢磨先生David2 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_454245032 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝2 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Charlie_lll2 天前
力扣解题-88. 合并两个有序数组
后端·算法·leetcode
菜鸡儿齐2 天前
leetcode-最小栈
java·算法·leetcode
岛雨QA2 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc2 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg12 天前
【算法】单调栈和单调队列
数据结构·算法
岛雨QA2 天前
图「Java数据结构与算法学习笔记12」
数据结构·算法
czxyvX2 天前
020-C++之unordered容器
数据结构·c++