-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathLinkedList.java
More file actions
48 lines (40 loc) · 889 Bytes
/
LinkedList.java
File metadata and controls
48 lines (40 loc) · 889 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
package datastructures;
public class LinkedList {
private Node head;
private int length = 0;
private class Node {
private final Object data;
private Node next;
Node(Object data) {
this.data = data;
}
}
public Object getHeadNode() {
return this.head.data;
}
public Object getTailNode() {
Node currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
return currentNode.data;
}
public Node insert(Object data) {
Node node = new Node(data);
if (this.head == null) {
this.head = node;
this.length++;
return node;
}
Node currentNode = this.head;
while (currentNode.next != null) {
currentNode = currentNode.next;
}
currentNode.next = node;
this.length++;
return node;
}
public int size() {
return this.length;
}
}