public class Node {
public int data;
public Node next;
public Node(){
data=0;
next=null;
}
public Node(int x){
data=x;
next=null;
}
}
The above code is the class Node, where it contains data
and next
variables.
Using this class observe the following code:
public class SinglyLinkedList {
public Node head=null;
public void insert_at_head(int x){
Node newnode=new Node(x);
if(head==null){
head=newnode;
}
else{
newnode.next=head;
head=newnode;
}
}
At line 2 head of type Node is initialized to null
. What is actually happening here? Is it equal to initializing head.data
and head.next
to null
?