# 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 not head or not head.next:
return head
pre, cur = None, head
while cur.next:
nex = cur.next
cur.next = pre
pre = cur
cur = nex
cur.next = pre
return cur
leetcode 206 反转链表
最新推荐文章于 2024-07-23 16:17:06 发布