2022-12-02 08:29:51 +00:00
|
|
|
# 总结
|
2022-12-02 09:24:26 +00:00
|
|
|
|
|
|
|
首先考虑递归 (e.g. s0206, s0024)
|
|
|
|
|
|
|
|
其次考虑双指针 (e.g. s0206, s0019, s0160)
|
|
|
|
|
|
|
|
递归遍历单链表:
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
void iter(ListNode *node) {
|
2022-12-02 11:12:03 +00:00
|
|
|
// 终止条件
|
2022-12-02 09:24:26 +00:00
|
|
|
if (node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
2022-12-02 11:12:03 +00:00
|
|
|
从前往后遍历
|
2022-12-02 09:24:26 +00:00
|
|
|
*/
|
|
|
|
iter(node->next);
|
2022-12-02 11:12:03 +00:00
|
|
|
/*
|
|
|
|
从后往前遍历
|
|
|
|
*/
|
|
|
|
return;
|
2022-12-02 09:24:26 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
递归遍历的意义在于让回溯单链表,也就是先遍历到结尾,然后从后往前遍历到某个 condition 。
|