前言
个人记录,仅代表个人观点,不接受任何反驳,代码逻辑能力不行,咱就先背句式,看着套吧
交换的基本句式
type a = 0, b = 1;
type tmp = a;
a = b;
b = tmp;
迭代的基本句式(链表反转)
type cur = head;
type prev = null;
while (cur != null) {
type tmp = cur.next;
cur.next = prev;
prev = cur;
cur = tmp;
}
这里就可以看出来,迭代是比交换的逻辑要复杂一些的,交换核心逻辑代码 三句,迭代的 四句
递归的基本句式(链表反转)
type func(type head) {
if (head == null || head.next == null) return head;
type prev = func(head.next);
head.next.next = head;
head.next = null;
return prev;
}
递归句式的核心代码
- 临界值,不能导致无限递归
- 递归调用自身
- 跳出前面一层递归所要做的操作