-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
50 lines (41 loc) · 835 Bytes
/
LinkedList.java
File metadata and controls
50 lines (41 loc) · 835 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package ch04;
public class LinkedList<T> {
private Node<T> head;
public Node<T> getHead() {
return this.head;
}
public void addAtStart(T data) {
Node<T> newNode = new Node<T>(data);
newNode.setNextNode(this.head);
this.head = newNode;
}
public Node<T> deleteAtStart() {
Node<T> toDel = this.head;
this.head = this.head.getNextNode();
return toDel;
}
public Node<T> find(T data) {
Node<T> curr = this.head;
while (curr != null) {
if (curr.getClass().equals(data)) {
return curr;
}
curr = curr.getNextNode();
}
return null;
}
public int length() {
if (head == null)
return 0;
int length = 0;
Node<T> curr = this.head;
while (curr != null) {
length += 1;
curr = curr.getNextNode();
}
return length;
}
public boolean isEmpty() {
return this.head == null;
}
}