Skip to content

Commit 3b93702

Browse files
authored
Add files via upload
1 parent 11adb3c commit 3b93702

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

RemoveLinkedListElements.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
return head;
28+
}
29+
}

0 commit comments

Comments
 (0)