# 206. Reverse Linked List

{% embed url="<https://leetcode.com/problems/reverse-linked-list/>" %}

经典链表操作，也算利用了双指针的思想：

初始化双指针，`prev`放`None`（完成reverse后的表尾），`cur`放`head`（现表头），然后用cur遍历链表：

1. 用一个临时对象`temp`暂存`cur`的下一个node`cur.next`
2. 把`cur`的指针指向`prev`（逆转`cur`和`prev`的指针方向）
3. 将`prev`和`cur`指针分别移动到`cur`和`temp`（即在原链表顺序上往后移动一格）

```python
# 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: ListNode) -> ListNode:
        prev = None
        cur = head
        while cur:
            follow = cur.next
            cur.next = prev
            prev = cur
            cur = follow
        return prev
```
