# 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==None or head.next==None :
return head
cur=head
pre=None
while cur:
tmp=cur.next
cur.next=pre
pre=cur
cur=tmp
return pre
LEETCODE 206. 反转链表
最新推荐文章于 2024-05-20 21:56:33 发布