-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.java
More file actions
45 lines (38 loc) · 985 Bytes
/
LinkedList.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
package com.codinginterview.dsa;
public class LinkedList<T extends Comparable<T>> implements Cloneable {
private Node<T> head = null;
public LinkedList(){
}
public int countNodes(){
if(head == null){
return 0;
}else{
Node<T> curr = head;
int count = 0;
while(curr != null){
curr = curr.getNext();
count++;
}
return count;
}
}
public void addNode(T data){
if (head == null){
head = new Node<T>(data);
}else{
Node<T> curr = head;
while(curr.getNext() != null){
curr = curr.getNext();
}
curr.setNext(new Node<T>(data));
}
}
public T popElement(){
if(head != null){
T topElement = head.getData();
head = head.getNext();
return topElement;
}
return null;
}
}