Types of Linked List | Java program to illustrate creation and traversal of Singly Linked List ; Structure of Node ; Function to print the content of linked list starting from the given node ; Iterate till n reaches null ; Print the data ; Driver Code ; Allocate 3 nodes in the heap ; Assign data in first node ; Link first node with second ; Assign data to second node ; Assign data to third node
class GFG { static class Node { int data ; Node next ; } ; static void printList ( Node n ) { while ( n != null ) { System . out . print ( n . data + " ▁ " ) ; n = n . next ; } } public static void main ( String [ ] args ) { Node head = null ; Node second = null ; Node third = null ; head = new Node ( ) ; second = new Node ( ) ; third = new Node ( ) ; head . data = 1 ; head . next = second ; second . data = 2 ; second . next = third ; third . data = 3 ; third . next = null ; printList ( head ) ; } }