-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathllistinsert.java
More file actions
46 lines (45 loc) · 1.18 KB
/
Copy pathllistinsert.java
File metadata and controls
46 lines (45 loc) · 1.18 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
public class LList
{
Node head;
static class Node{
int data;
Node next;
Node(int d){
this.data = d;
next = null;
}
}
public static LList insertAData(LList llist, int data){
Node newNode = new Node(data);
newNode.next = null;
if(llist.head==null){
llist.head=newNode;
}else{
Node lastNode = llist.head;
while(lastNode.next!=null){
lastNode = lastNode.next;
}
lastNode.next = newNode;
}
return llist;
}
public static void printLlist(LList llist){
Node currNode = llist.head;
while(currNode!=null){
System.out.println("Data "+currNode.data);
currNode = currNode.next;
}
}
public static void main(String[] args) {
LList myllist = new LList();
insertAData(myllist, 1);
insertAData(myllist, 2);
insertAData(myllist, 3);
insertAData(myllist, 4);
insertAData(myllist, 5);
insertAData(myllist, 6);
insertAData(myllist, 7);
insertAData(myllist, 8);
printLlist(myllist);
}
}