-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm203.java
More file actions
45 lines (43 loc) · 1.22 KB
/
prblm203.java
File metadata and controls
45 lines (43 loc) · 1.22 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
public class prblm203 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(6);
ListNode l4 = new ListNode(3);
ListNode l5 = new ListNode(4);
ListNode l6 = new ListNode(5);
ListNode l7 = new ListNode(6);
ListNode head1 = l1;
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = l6;
l6.next = l7;
printNode(head1);
ListNode newHead = removeElements(head1, 6);
printNode(newHead);
}
public static void printNode(ListNode node){
ListNode curr = node;
while(curr != null){
System.out.print(curr.val + " ");
curr = curr.next;
}
System.out.println();
}
public static ListNode removeElements(ListNode head, int val) {
ListNode newNode = new ListNode(0);
newNode.next = head;
ListNode curr = newNode;
while (curr.next != null) {
if(curr.next.val == val){
curr.next = curr.next.next;
}
else{
curr = curr.next;
}
}
return newNode.next;
}
}