public class LinkedList {
	Node head;

	class Node {
		int data;
		// Reference to next node
		Node next;

		// constructor to create object and initialialized object
		Node(int data) {
			this.data = data;
			next = null;
		}
	}

	void printlink() {
		Node currentNode = head;
		while (currentNode != null) {
			System.out.println(currentNode.data);
			currentNode = currentNode.next;
		}
	}

	public static void main(String str[]) {
		LinkedList ll = new LinkedList();

		// now to create an object of node we need object of outer class
		Node first = ll.new Node(1);
		// point to head node
		ll.head = first;

		Node secondNode = ll.new Node(2);
		// so first should point to the next node
		first.next = secondNode;

		Node thirdNode = ll.new Node(3);
		// so second should point to the next node i.e the third node
		secondNode.next = thirdNode;
		ll.printlink();

	}
}
50% LikesVS
50% Dislikes

Leave a Reply

Your email address will not be published. Required fields are marked *