-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinkedList.java
More file actions
67 lines (62 loc) · 1.79 KB
/
MyLinkedList.java
File metadata and controls
67 lines (62 loc) · 1.79 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
57
58
59
60
61
62
63
64
65
66
67
public class MyLinkedList<E extends Comparable<E>> {
private Node<E> head;
public void append(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
} else {
Node<E> current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
public MyLinkedList<E> getFiltered(E threshold) {
MyLinkedList<E> filteredList = new MyLinkedList<>();
Node<E> current = head;
while (current != null) {
if (current.data.compareTo(threshold) > 0) {
filteredList.append(current.data);
}
current = current.next;
}
return filteredList;
}
public E getMax() {
if (head == null) return null;
E max = head.data;
Node<E> current = head.next;
while (current != null) {
if (current.data.compareTo(max) > 0) {
max = current.data;
}
current = current.next;
}
return max;
}
public E getMin() {
if (head == null) return null;
E min = head.data;
Node<E> current = head.next;
while (current != null) {
if (current.data.compareTo(min) < 0) {
min = current.data;
}
current = current.next;
}
return min;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
Node<E> current = head;
while (current != null) {
sb.append(current.data);
if (current.next != null) sb.append(", ");
current = current.next;
}
sb.append("]");
return sb.toString();
}
}