forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListRemoveDuplicate.java
67 lines (60 loc) · 1.23 KB
/
LinkedListRemoveDuplicate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/03/linked-list-remove-duplicates.html
*/
package com.dsalgo;
import java.util.HashSet;
public class LinkedListRemoveDuplicate
{
public static void main(String[] args)
{
Chain input = new Chain();
input.add(3).add(5).add(4).add(2).add(3).add(2).add(6).add(3).add(4);
Chain output = removeDuplicate(input);
for (Node node = output.head; node != null; node = node.next)
System.out.print(node.value + "->");
System.out.println();
}
private static Chain removeDuplicate(Chain input)
{
Chain output = new Chain();
HashSet<Integer> hashSet = new HashSet<Integer>();
for (Node node = input.head; node != null; node = node.next)
{
if (!hashSet.contains(node.value))
{
hashSet.add(node.value);
output.add(node.value);
}
}
return output;
}
public static class Node
{
int value;
Node next;
public Node(int value)
{
this.value = value;
}
}
private static class Chain
{
Node head;
Node last;
public Chain add(int value)
{
Node node = new Node(value);
if (head == null)
{
head = node;
last = node;
} else
{
last.next = node;
last = node;
}
return this;
}
}
}