add first in a linked list
public class addFirst {
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public void addFirstInaLL(int data) {
// CREATION OF NODE
Node newNode = new Node(data);
// IF LL IS EMPTY
if (head == null) {
head = tail = newNode;
return;
}
newNode.next = head;
head = newNode;
}
// PRINTING A LL
public void print() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data);
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
Linkedlist ll = new Linkedlist();
ll.addFirst(1);
ll.addFirst(8);
ll.addFirst(2);
ll.addFirst(5);
ll.print();
}
}
Comments
Post a Comment