-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linkedlist Insertion1.txt
51 lines (42 loc) · 1.21 KB
/
Linkedlist Insertion1.txt
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
LINKED LIST
Insertion of node at first position:---
output--->40-->30-->20-->10-->null
public class LinkedList
{
private ListNode head;
public static class ListNode
{
private int data;
private ListNode next;
public ListNode(int data)
{
this.data=data;
this.next=null;//by default it is null.
}
}
// insert at first position of the linked list....
public void insertFirst(int value)
{
ListNode newNode=new ListNode(value);
newNode.next=head;
head=newNode;
}
public void display()
{
ListNode n=head;
while(n!=null)
{
System.out.print(n.data+"-->");
n=n.next;
}
System.out.print("null");
}
public static void main (String[] args)
{
LinkedList sll=new LinkedList();
sll.insertFirst(10);
sll.insertFirst(20);
sll.insertFirst(30);
sll.insertFirst(40);
sll.display();
}