-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
64 lines (49 loc) · 1.11 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package JAVA;
public class LinkedList{
Node head = null;
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add(1);
ll.add(2);
ll.add(3);
ll.print();
}
void add(int data){
Node node = new Node(data);
if(head == null){
head = node;
return;
}
Node currentNode = head;
while(currentNode.next != null){
currentNode = currentNode.next;
}
currentNode.next = node;
}
void print(){
while (head != null){
System.out.println(head.getData());
head = head.getNext();
}
}
}
class Node{
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}