-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
68 lines (59 loc) · 2.32 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.IOException;
import java.util.Scanner;
public class solution {
//Class for Node
static class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
//Class for Linked List
static class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedList() {
this.head = null;
}
}
//Member method to print the data of the nodes of singlyLinkedList
public static void printSinglyLinkedList(SinglyLinkedListNode node) throws IOException {
while (node != null) {
System.out.print(node.data + " -> ");
node = node.next;
}
System.out.println("NULL");
}
//Member method to insert Node at Head
static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) {
SinglyLinkedListNode temp = new SinglyLinkedListNode(data);
//If head is null simply point head to temp and return head
if (llist == null) {
llist = temp;
return llist;
}
//Point next of the new node created to head
temp.next = llist;
//Point head to temp
llist = temp;
//Return the head
return llist;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
//Creating Singly Linked List
SinglyLinkedList llist = new SinglyLinkedList();
//Code for adding node at head of Linked list
int llistCount = scanner.nextInt();
for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
SinglyLinkedListNode llist_head = insertNodeAtHead(llist.head, llistItem);
llist.head = llist_head;
}
//Call to Method to print out the Linked List
printSinglyLinkedList(llist.head);
scanner.close();
}
}
}