- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.java
More file actions
Latest commit
45 lines (38 loc) · 985 Bytes
/
Copy pathLinkedList.java
File metadata and controls
45 lines (38 loc) · 985 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
packagecom.codinginterview.dsa;
publicclassLinkedList<TextendsComparable<T>> implementsCloneable {
privateNode<T> head = null;
publicLinkedList(){
}
publicintcountNodes(){
if(head == null){
return0;
}else{
Node<T> curr = head;
intcount = 0;
while(curr != null){
curr = curr.getNext();
count++;
}
returncount;
}
}
publicvoidaddNode(Tdata){
if (head == null){
head = newNode<T>(data);
}else{
Node<T> curr = head;
while(curr.getNext() != null){
curr = curr.getNext();
}
curr.setNext(newNode<T>(data));
}
}
publicTpopElement(){
if(head != null){
TtopElement = head.getData();
head = head.getNext();
returntopElement;
}
returnnull;
}
}