From 50be5980defbaa3b7d6564688f6e7f45e99a331d Mon Sep 17 00:00:00 2001 From: 1706581 <45617500+1706581@users.noreply.github.com> Date: Thu, 1 Oct 2020 00:56:59 +0530 Subject: [PATCH] Linked List Java Code highly readable --- MyLinkedList.java | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 MyLinkedList.java diff --git a/MyLinkedList.java b/MyLinkedList.java new file mode 100644 index 00000000..e37993b8 --- /dev/null +++ b/MyLinkedList.java @@ -0,0 +1,61 @@ +package dynamic_programming; + + + +public class MyLinkedList { + + + + class Node { + int data; + Node next; + + Node(int d) { + data = d; + next = null; + } + } + + + + + Node insert(Node head, int x) { + Node nn = new Node(x); + + if (head == null) { + head = nn; + } else { + Node curr = head; + while (curr.next != null) { + + curr = curr.next; + } + curr.next = nn; + } + + return head; + + } + + void print(Node head){ + Node curr = head; + while(curr != null) { + System.out.print(curr.data+" "); + curr =curr.next; + } + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + MyLinkedList l = new MyLinkedList(); + Node head=null;; + + head = l.insert(head, 9); + head = l.insert(head, 10); + head = l.insert(head, 11); + head = l.insert(head, 12); + + l.print(head); + } + +}