-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.java
44 lines (37 loc) · 926 Bytes
/
LinkedList.java
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
package datastructures;
public class LinkedList<T> {
private Node<T> head;
public void addToEnd(T data) {
Node<T> newNode = new Node<>(data);
if (head == null) {
head = newNode;
return;
}
Node<T> current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
public void addToBeginning(T data) {
Node<T> newNode = new Node<>(data);
newNode.next = head;
head = newNode;
}
public void display() {
Node<T> current = head;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
// Implement other methods like search, delete, etc.
}
class Node<T> {
T data;
Node<T> next;
Node(T data) {
this.data = data;
this.next = null;
}
}