-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomLinkedList.java
More file actions
44 lines (37 loc) · 1.02 KB
/
CustomLinkedList.java
File metadata and controls
44 lines (37 loc) · 1.02 KB
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
import java.util.Scanner;
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class CustomLinkedList {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Node head = null, tail = null;
System.out.print("How many elements? ");
int n = sc.nextInt();
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
int value = sc.nextInt();
Node newNode = new Node(value);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
// Print the linked list
System.out.println("Linked List elements:");
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("null");
}
}