-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddingNodeAtLast.java
More file actions
65 lines (62 loc) · 1.04 KB
/
AddingNodeAtLast.java
File metadata and controls
65 lines (62 loc) · 1.04 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
55
56
57
58
59
60
61
62
63
64
65
class Linkedlist
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
this.data = d;
this.next = null;
}
}
public static void main(String[] args)
{
Linkedlist ll = new Linkedlist();
ll.head = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
Node fourth = new Node(40);
ll.head.next = second;
second.next = third;
third.next = fourth;
ll.Insertfirst();
ll.InsertLast();
ll.printlist();
}
public void printlist()
{
Node n = head;
while(n!=null)
{
System.out.print(n.data + "->");
n = n.next;
}
}
public void Insertfirst()
{
Linkedlist ll = new Linkedlist();
Node firstNode = new Node(5);
firstNode.next = head;
head = firstNode;
}
public void InsertLast()
{
Linkedlist ll = new Linkedlist();
Node lastNode = new Node(45);
if(head == null)
{
head = lastNode;
return;
}
lastNode.next = null;
Node start = head;
while(start.next!= null)
{
start = start.next;
}
start.next = lastNode;
}
}
//Output : 5->10->20->30->40->45->