# 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]:
if head is None :
return None
cur = head
if cur.next is None:
return head
nxt = cur.next
pre = None
while nxt is not None :
cur.next = pre
pre = cur
cur = nxt
nxt = cur.next
cur.next = pre
return cur
2025.08.08 反转链表
最新推荐文章于 2026-08-20 11:34:55 发布

446

被折叠的 条评论
为什么被折叠?



