1. 题目

【LeetCode】剑指 Offer II 024. 反转链表_链表

2. 分析

思路一定要清晰,思路一定要清晰。

3. 代码

# 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: ListNode) -> ListNode:
start = head

pre = None # 初始节点为None
# 开始翻转
while(head):
nx = head.next
head.next = pre
pre = head
head = nx
return