-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob5.java
More file actions
33 lines (28 loc) · 810 Bytes
/
prob5.java
File metadata and controls
33 lines (28 loc) · 810 Bytes
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
import java.util.HashSet;
public class prob5{
public static void main(String[] args) {
System.out.println("Hello world");
int[] arr = new int[] {1,1,2,3,3, 4,5,6};
ListNodeInt head = LinkedListUtil.convertToLinkedList(arr);
LinkedListUtil.print(head);
head = remove_dupbs(head);
LinkedListUtil.print(head);
}
public static ListNodeInt remove_dupbs(ListNodeInt head){
ListNodeInt newHead = new ListNodeInt(-1, head);
ListNodeInt cursor = newHead.next;
ListNodeInt prev = newHead;
HashSet<Integer> set = new HashSet<>();
while(cursor!= null){
if(set.contains(cursor.val)){
prev.next = cursor.next;
}
else{
set.add(cursor.val);
prev = cursor;
}
cursor = cursor.next;
}
return newHead.next;
}
}