leetcode hot100 反转链表

# 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]:
        prev = None
        cur= head
        while cur:
            tmp =cur.next
            cur.next = prev
            prev = cur
            cur = tmp
        return prev

模板,背把老弟

你可能感兴趣的:(leetcode,链表,算法)