-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteNode.java
More file actions
66 lines (57 loc) · 1.78 KB
/
deleteNode.java
File metadata and controls
66 lines (57 loc) · 1.78 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package LinkedList;
import java.util.Scanner;
public class deleteNode {
public static class Node {
int val;
Node next;
}
public static class LinkedList {
Node head;
Node tail;
void create() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the linked list: ");
int size = sc.nextInt();
if (size == 0) {
System.out.println("Empty list created.");
return;
}
System.out.println("Enter the elements:");
for (int i = 0; i < size; i++) {
int value = sc.nextInt();
Node newNode = new Node();
newNode.val = value;
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
}
}
void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.val + " ");
temp = temp.next;
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList ll = new LinkedList();
ll.create();
System.out.print("Enter the index you want to delete: ");
int index = sc.nextInt();
Node temp = ll.head;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
temp.val = temp.next.val;
temp.next = temp.next.next;
System.out.print("Linked List is: ");
ll.display();
}
}