-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaSLL.java
More file actions
51 lines (46 loc) · 1.27 KB
/
JavaSLL.java
File metadata and controls
51 lines (46 loc) · 1.27 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
class JavaSLL {
public Node head;
public JavaSLL() {
this.head = null;
}
public JavaSLL(int value) {
this.head = new Node(value);
}
public void add(int value) {
Node newNode = new Node(value);
if(this.head == null){
this.head = newNode;
} else{
Node current = head;
while(current.next != null){
current = current.next;
}
current.next = newNode;
}
}
public void remove() {
if(this.head.next == null){
this.head = null;
} else{
Node current = this.head;
int counter = 1;
while(current.next != null){
current = current.next;
}
current = null;
}
}
public void printValues() {
if(this.head == null){
System.out.println("No nodes in this list!");
} else{
Node current = this.head;
int counter = 1;
while(current != null) {
System.out.println("Node " + counter + ": " + current.val);
counter ++;
current = current.next;
}
}
}
}