From ea8cf143294f975b5effe93eb774434ea317efef Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 8 Feb 2021 13:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9B=E6=89=A3=EF=BC=9A=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E9=A2=98=2002.01.=20=E7=A7=BB=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../study/t20210207/RemoveDuplicateNodes.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 LeetCode/src/main/java/com/code/leet/study/t20210207/RemoveDuplicateNodes.java diff --git a/LeetCode/src/main/java/com/code/leet/study/t20210207/RemoveDuplicateNodes.java b/LeetCode/src/main/java/com/code/leet/study/t20210207/RemoveDuplicateNodes.java new file mode 100644 index 0000000..30f442b --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/study/t20210207/RemoveDuplicateNodes.java @@ -0,0 +1,30 @@ +package com.code.leet.study.t20210207; + +import com.code.leet.entiy.ListNode; + +import java.util.ArrayList; +import java.util.List; + +/** + * 编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。 + */ +public class RemoveDuplicateNodes { + /** + * 面试题 02.01. 移除重复节点 + */ + public ListNode removeDuplicateNodes(ListNode head) { + List list = new ArrayList<>(); + ListNode temp = head; + ListNode pro = null; + while (temp != null) { + if (list.contains(temp.val)) { + pro.next = temp.next; + } else { + list.add(temp.val); + pro = temp; + } + temp = temp.next; + } + return head; + } +}