From e3f4f3c0c8bbce2c1e369b869a8ce30f4253f91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=B7=AF=E7=94=9F?= <312885991@qq.com> Date: Sat, 25 Feb 2023 14:24:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200206.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8=20=E5=8D=95=E7=BA=AF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=99=9A=E6=8B=9F=E5=A4=B4=E7=BB=93=E7=82=B9=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=93=BE=E8=A1=A8=E7=BF=BB=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 016477ae..b87c7cba 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -682,7 +682,33 @@ public class LinkNumbers +## 使用虚拟头结点解决链表翻转 + +> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈) + +```java +// 迭代方法:增加虚头结点,使用头插法实现链表翻转 +public static ListNode reverseList1(ListNode head) { + // 创建虚头结点 + ListNode dumpyHead = new ListNode(-1); + dumpyHead.next = null; + // 遍历所有节点 + ListNode cur = head; + while(cur != null){ + ListNode temp = cur.next; + // 头插法 + cur.next = dumpyHead.next; + dumpyHead.next = cur; + cur = temp; + } + return dumpyHead.next; +} +``` + + + ## 使用栈解决反转链表的问题 + * 首先将所有的结点入栈 * 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。 @@ -720,3 +746,4 @@ public ListNode reverseList(ListNode head) { + From 6043be28ff4692d8bee1218f62b92fca5175d739 Mon Sep 17 00:00:00 2001 From: Jeremy Feng <44312563+jeremy-feng@users.noreply.github.com> Date: Mon, 27 Feb 2023 10:35:47 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200001.=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 Typo --- problems/0001.两数之和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 2d3d6ea3..a0acdcbe 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -79,7 +79,7 @@ map目的用来存放我们访问过的元素,因为遍历数组的时候, 所以 map中的存储结构为 {key:数据元素,value:数组元素对应的下标}。 -在遍历数组的时候,只需要向map去查询是否有和目前遍历元素比配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。 +在遍历数组的时候,只需要向map去查询是否有和目前遍历元素匹配的数值,如果有,就找到的匹配对,如果没有,就把目前遍历的元素放进map中,因为map存放的就是我们访问过的元素。 过程如下: From 94d4af7d916c0770d6824898efc7b017b91d5041 Mon Sep 17 00:00:00 2001 From: roylx <73628821+roylx@users.noreply.github.com> Date: Mon, 27 Feb 2023 13:51:47 -0700 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86python=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E7=A9=BA=E9=97=B4=E5=A4=8D=E6=9D=82=E5=BA=A6?= =?UTF-8?q?=E7=9A=84=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0501.二叉搜索树中的众数.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index cb14fb14..4214e232 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -476,6 +476,7 @@ class Solution { ## Python > 递归法 +> 常量空间,递归产生的栈不算 ```python # Definition for a binary tree node. @@ -521,7 +522,9 @@ class Solution: ``` -> 迭代法-中序遍历-不使用额外空间,利用二叉搜索树特性 +> 迭代法-中序遍历 +> 利用二叉搜索树特性,在历遍过程中更新结果,一次历遍 +> 但需要使用额外空间存储历遍的节点 ```python class Solution: def findMode(self, root: TreeNode) -> List[int]: