From 58a69f22f4b04f5c9895faca6d50634c42a84111 Mon Sep 17 00:00:00 2001 From: NIRALI VAGHELA <61160793+nv182001@users.noreply.github.com> Date: Sun, 18 Oct 2020 15:56:50 +0530 Subject: [PATCH] Create linkedlist Linked List Traversal --- linkedlist | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 linkedlist diff --git a/linkedlist b/linkedlist new file mode 100644 index 0000000..396c1e9 --- /dev/null +++ b/linkedlist @@ -0,0 +1,42 @@ +// A simple Java program for traversal of a linked list +class LinkedList { + Node head; // head of list + + /* Linked list Node. This inner class is made static so that + main() can access it */ + static class Node { + int data; + Node next; + Node(int d) + { + data = d; + next = null; + } // Constructor + } + + /* This function prints contents of linked list starting from head */ + public void printList() + { + Node n = head; + while (n != null) { + System.out.print(n.data + " "); + n = n.next; + } + } + + /* method to create a simple linked list with 3 nodes*/ + public static void main(String[] args) + { + /* Start with the empty list. */ + LinkedList llist = new LinkedList(); + + llist.head = new Node(1); + Node second = new Node(2); + Node third = new Node(3); + + llist.head.next = second; // Link first node with the second node + second.next = third; // Link second node with the third node + + llist.printList(); + } +}