From 8ad2f18126919f8be221e00cb5962b54c55a6a12 Mon Sep 17 00:00:00 2001 From: whai Date: Wed, 27 Dec 2023 19:44:46 +0800 Subject: [PATCH] =?UTF-8?q?LeetCode203=20707=E9=93=BE=E8=A1=A8=20-=20?= =?UTF-8?q?=E5=8F=8C=E5=90=91=E9=93=BE=E8=A1=A8=E7=9A=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=20-=20=E5=A2=9E=E5=8A=A0=E8=99=9A=E6=8B=9F=E5=A4=B4=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=96=B9=E4=BE=BF=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/easy/LeetCode203.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/cn/whaifree/leetCode/easy/LeetCode203.java b/src/main/java/cn/whaifree/leetCode/easy/LeetCode203.java index a434b79..c88c6b8 100644 --- a/src/main/java/cn/whaifree/leetCode/easy/LeetCode203.java +++ b/src/main/java/cn/whaifree/leetCode/easy/LeetCode203.java @@ -174,13 +174,38 @@ public class LeetCode203 { } + /** + * 0 1 2 3 + * 加上虚拟表头节点 + * @param head + * @param val + * @return + */ + public ListNode removeElements7(ListNode head, int val) { + ListNode virtualHead = new ListNode(0, head); + ListNode pre = virtualHead; + ListNode index = head; + head = virtualHead; + while (index != null) { + if (index.val == val) { + pre.next = index.next; + index = pre.next; + }else { + pre = pre.next; + index = index.next; + } + } + return head.next; + } - @Test + + + @Test public void Test() { - ListNode listNode = ListNode.listNodeFromArray(new int[]{6,6,6,6}); + ListNode listNode = ListNode.listNodeFromArray(new int[]{7,7,7,7}); // ListNode.printList(listNode); - ListNode listNode1 = removeElements(listNode, 6); + ListNode listNode1 = removeElements7(listNode, 7); ListNode.printList(listNode1); } }