How to print a linked list elements in java

0
 Linked list is a most important data structure of the java. One of the most basic thing from this data structure is to print the elements . 



Input  : 1  -> 2 -> 3 -> 4 -> null
Output : 1 2 3 4
Input : 1 -> null
Output : 1
Input : null
Output : null


Java Code :



class Node{
int data;
NOde next;
Node(int data){
this.data=data;
this.next=null;
}
}

class HindiCodingCommunity{

public static void print(Node head){
Node curr=head;
while(curr!=null){
System.out.println(curr.data);
curr=curr.next;
}
}
public static void main(String args[])
{
Node head= new Node(5);
head.next= new Node(7);
head.next.next = new Node(8);
head.next.next.next = new Node(9);

print(head);
}
}


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !