We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 11adb3c commit 3b93702Copy full SHA for 3b93702
RemoveLinkedListElements.java
@@ -0,0 +1,29 @@
1
+public class RemoveLinkedListElements {
2
+
3
+ public ListNode removeElements(ListNode head, int val) {
4
+ if(head==null) {
5
+ return head;
6
+ }
7
8
+ //skip heads with same value
9
+ while(head!=null && head.val==val) {
10
+ head=head.next;
11
12
13
+ ListNode temp=head;
14
15
+ //delete same value nodes
16
+ while(temp!= null && temp.next!=null) {
17
18
+ //for the corner case 1 2 2 1
19
+ //we have to remove all the middle elements
20
21
+ while(temp.next!=null && temp.next.val==val) {
22
+ temp.next=temp.next.next;
23
24
+ temp=temp.next;
25
26
27
28
29
+}
0 commit comments