forked from Revnth/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LL.java
48 lines (48 loc) · 1.13 KB
/
LL.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
public class LL{
Node head;
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
public void addFirst(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
return;
}
newNode.next=head;
head=newNode;
}
public void addLast(int data){
Node newNode=new Node(data);
if(head==null){
head=newNode;
return;
}
Node currNode=head;
while(currNode.next!=null){
currNode=currNode.next;
}
currNode.next=newNode;
}
public void printList(){
Node currNode=head;
while(currNode!=null){
System.out.println(currNode.data);
currNode=currNode.next;
}
}
public static void main(String[] args){
LL list=new LL();
list.addFirst(3);
list.addFirst(2);
list.addFirst(1);
list.addLast(4);
list.addFirst(0);
list.printList();
}
}