Last active
December 11, 2022 08:45
-
-
Save medmek/bbc23a64764aab68dd07939e03572597 to your computer and use it in GitHub Desktop.
Day 15: Linked List (Java) Hackerrank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
class Node { | |
int data; | |
Node next; | |
Node(int d) { | |
data = d; | |
next = null; | |
} | |
} | |
class Solution { | |
public static Node insert(Node head,int data) { | |
if(head == null){ | |
head = new Node(data); | |
return head; | |
} | |
Node start = head; | |
while(start.next != null){ | |
start = start.next; | |
} | |
start.next = new Node(data); | |
return head; | |
} | |
public static void display(Node head) { | |
Node start = head; | |
while(start != null) { | |
System.out.print(start.data + " "); | |
start = start.next; | |
} | |
} | |
public static void main(String args[]) { | |
Scanner sc = new Scanner(System.in); | |
Node head = null; | |
int N = sc.nextInt(); | |
while(N-- > 0) { | |
int ele = sc.nextInt(); | |
head = insert(head,ele); | |
} | |
display(head); | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good job