-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.java
42 lines (33 loc) · 1.01 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
public class LinkedList {
//creating a function for creation of node
public static class Node {
int data;
Node next;
//setting the type of linked list
public Node(int data) {
this.data = data;
this.next = null;
}
}
//setting the beginning and end of LL
public static Node head;
public static Node tail;
//creating method to add values to nodes
public void addfirst(int data) {
//creating new node
Node newNode = new Node(data);//creating an object of class node
if(head == null) {
head = tail = newNode;
return;
}
//assigning the value to the nxt node
newNode.next = head;//here one node is linked to another node
//assigning the value of head to new nodw
head = newNode;
}
public static void main(String args[]) {
LinkedList li = new LinkedList();
li.addfirst(1);
li.addfirst(2);
}
}