-
Notifications
You must be signed in to change notification settings - Fork 0
/
ques15.java
66 lines (52 loc) · 1.42 KB
/
ques15.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
//Removing/Deleting the particular value from the linkedlist
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
class LinkedlistOperations {
static void print(Node head) {
Node curr = head;
if (head == null) {
System.out.println(head);
return;
}
while (curr != null) {
System.out.print(curr.data + " -> ");
curr = curr.next;
}
System.out.println();
}
static Node removeElements(Node head, int val) {
while (head != null && head.data == val) {
head = head.next;
}
if (head == null) {
return null;
}
Node prev = head;
Node curr = head.next;
while (curr != null) {
if (curr.data == val) {
prev.next = curr.next;
} else {
prev = curr;
}
curr = curr.next;
}
return head;
}
}
public class ques15 {
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(1);
head.next.next = new Node(1);
LinkedlistOperations.print(head);
System.out.println("Modified");
head = LinkedlistOperations.removeElements(head, 1);
LinkedlistOperations.print(head);
}
}