HOT 100 › 索引 › B7 链表 Linked List
LC 206反转链表Reverse Linked List 简单
给定单链表头节点 head,把链表反转并返回新头节点。
思路 迭代三指针:pre/cur 逐个把 next 指向前驱。时间 O(n),空间 O(1)。
class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: pre, cur = None, head while cur: nxt = cur.next cur.next = pre pre = cur cur = nxt return pre