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

相关推荐
木井巳13 分钟前
【动态规划: 线性dp】解码方法
java·算法·leetcode·动态规划·推荐算法
春弦_8 小时前
自用算法整理二
数据结构·算法·图论
java_nnnn9 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
Navigator_Z10 小时前
LeetCode //C - 1301. Number of Paths with Max Score
c语言·算法·leetcode
Nuanyt13 小时前
LeetCode Hot100 刷题笔记 · 普通数组专题
java·笔记·算法·leetcode
xxxiugou12313 小时前
链表算法题精讲:从两数相加到K组翻转
数据结构·c++·算法·链表
春弦_14 小时前
LeetCode Hot 100 刷题笔记
笔记·leetcode·哈希算法
凯瑟琳.奥古斯特17 小时前
力扣1015C++解法技巧
c++·算法·leetcode·链表·职场和发展
悠仁さん17 小时前
【算法C++】双指针(一)
数据结构·c++·算法
文丑颜不良啊17 小时前
LeetCode626~650题目解析
java·leetcode