-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
53 lines (44 loc) · 1 KB
/
LinkedList.java
File metadata and controls
53 lines (44 loc) · 1 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
package FloydsCycleDetection;
public class LinkedList {
Node head, tail;
int size;
public LinkedList(){
this.head = null;
this.tail = null;
this.size = 0;
}
public void add(Node newNode){
if(head == null) {
head = newNode;
tail = newNode;
}
else{
tail.next = newNode;
tail = tail.next;
}
size++;
}
public int size(){
return size;
}
public void print() {
Node current = head;
while (current != tail) {
System.out.print(current);
current = current.next;
}
System.out.print(current);
System.out.println();
}
public Node getNode(int id) {
Node current = head;
if(tail.id == id)
return tail;
while (current != tail) {
if(current.id == id)
return current;
current = current.next;
}
return null;
}
}