From b579f2f6c27dc12a3b85a3374683a63b9f887025 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Sat, 28 Aug 2021 11:21:18 -0700 Subject: [PATCH] add solution and testcase for 1836 --- .../java/com/fishercoder/solutions/_1836.java | 31 +++++++++++++++++++ src/test/java/com/fishercoder/_1836Test.java | 25 +++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/com/fishercoder/solutions/_1836.java create mode 100644 src/test/java/com/fishercoder/_1836Test.java diff --git a/src/main/java/com/fishercoder/solutions/_1836.java b/src/main/java/com/fishercoder/solutions/_1836.java new file mode 100644 index 0000000000..2631a1d69a --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1836.java @@ -0,0 +1,31 @@ +package com.fishercoder.solutions; + +import com.fishercoder.common.classes.ListNode; + +import java.util.HashSet; +import java.util.Set; + +public class _1836 { + public static class Solution1 { + public ListNode removeDuplicates(ListNode head) { + // write your code here + // Maintain a hashtable to checkup for duplicate element in constant time + Set uniqueNums = new HashSet<>(); + + ListNode current = head; + ListNode prev = null; + while (current != null) { + int val = current.val; + if (uniqueNums.contains(val)) { + prev.next = current.next; + } + else { + uniqueNums.add(val); + prev = current; + } + current = current.next; + } + return head; + } + } +} diff --git a/src/test/java/com/fishercoder/_1836Test.java b/src/test/java/com/fishercoder/_1836Test.java new file mode 100644 index 0000000000..1fb98c0d0d --- /dev/null +++ b/src/test/java/com/fishercoder/_1836Test.java @@ -0,0 +1,25 @@ +package com.fishercoder; + +import com.fishercoder.common.classes.ListNode; +import com.fishercoder.solutions._1836; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class _1836Test { + private static _1836.Solution1 solution1; + private static ListNode head; + private static ListNode result; + @BeforeClass + public static void setup() { solution1 = new _1836.Solution1(); } + + @Test + public void test1() { + head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 1, 3, 3, 5, 6, 3)); + result = solution1.removeDuplicates(head); + assertEquals(result, ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 5, 6))); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy