From 09c1bd818abf8da2c1c426a06e46f2063b0089b0 Mon Sep 17 00:00:00 2001 From: Aaron-Lin-74 <84072071+Aaron-Lin-74@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:12:33 +1100 Subject: [PATCH] =?UTF-8?q?Update=200203.=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Creating a dummy head variable makes the logic clear. --- problems/0203.移除链表元素.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 4e7bdd34..d2ba1ae8 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -342,14 +342,14 @@ function removeElements(head: ListNode | null, val: number): ListNode | null { ```typescript function removeElements(head: ListNode | null, val: number): ListNode | null { - head = new ListNode(0, head); - let pre: ListNode = head, cur: ListNode = head.next; + let dummyHead = new ListNode(0, head); + let pre: ListNode = dummyHead, cur: ListNode | null = dummyHead.next; // 删除非头部节点 while (cur) { if (cur.val === val) { pre.next = cur.next; } else { - pre = pre.next; + pre = cur; } cur = cur.next; }