Skip to content

Instantly share code, notes, and snippets.

@medmek
Last active December 11, 2022 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medmek/bbc23a64764aab68dd07939e03572597 to your computer and use it in GitHub Desktop.
Save medmek/bbc23a64764aab68dd07939e03572597 to your computer and use it in GitHub Desktop.
Day 15: Linked List (Java) Hackerrank
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();
}
}
@ShifasCollections
Copy link

good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment