-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq2p1.java
More file actions
56 lines (52 loc) · 1.28 KB
/
Copy pathq2p1.java
File metadata and controls
56 lines (52 loc) · 1.28 KB
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
import java.util.*;
import java.lang.*;
// T: O(n)
// S: O(n)
// B: another way is using two pointers, just like brute force, T: O(n^2), S:O(1)
public class q2p1{
public static void main(String[] args){
Node head = new Node(1);
Random rd = new Random();
Set<Integer> hs = new HashSet<>();
int n = 10;
while(n>0){
head.append(rd.nextInt(8));
n--;
}
Node buffer = head;
while(buffer.next !=null){
System.out.println(buffer.value);
buffer = buffer.next;
}
buffer = head;
Node previous = null;
while(buffer.next != null){
if(!hs.add(buffer.value)){
previous.next = buffer.next;
}else{
previous = buffer;
}
buffer = buffer.next;
}
buffer = head;
while(buffer.next !=null){
System.out.println(">> " + buffer.value);
buffer = buffer.next;
}
}
}
class Node{
int value;
Node next = null;
public Node(int n){
value = n;
}
public void append(int n){
Node end = new Node(n);
Node k = this;
while(k.next != null){
k = k.next;
}
k.next = end;
}
}