-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
54 lines (47 loc) · 1.14 KB
/
LinkedList.java
File metadata and controls
54 lines (47 loc) · 1.14 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
53
54
public class LinkedList {
public class Node {
String data;
Node next;
Node(String data) {
this.data = data;
next = null;
}
}
// add at first
private Node head;
public void addFirst(String data) {
Node newNode = new Node(data);
if (head == null) {
newNode.next = null;
head = newNode;
return;
}
newNode.next = head;
head = newNode;
}
public void printList() {
Node s = head;
if (s == null) {
System.out.println("No Elements: ");
return;
}
while (s != null) {
System.out.print(s.data);
if (s.next != null) {
System.out.print(" -> ");
}
s = s.next;
}
System.out.println();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.printList();
list.addFirst("A");
list.printList();
list.addFirst("B");
list.printList();
list.addFirst("C");
list.printList();
}
}