-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion203.java
More file actions
48 lines (43 loc) · 885 Bytes
/
Copy pathQuestion203.java
File metadata and controls
48 lines (43 loc) · 885 Bytes
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
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 203
*/
public class Question203
{
public static ListNode removeElements(ListNode head, int val)
{
if(head==null)
return null;
ListNode temp = head;
ListNode previous = null;
boolean flag = true;
while(temp!=null)
{
if(temp.val == val && previous==null)
{
head = temp.next;
previous = null;
}
else if(temp.val == val)
{
previous.next = temp.next;
}
else if(temp.val!=val)
{
previous = temp;
}
temp = temp.next;
}
return head;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
ListNode head = LinkedList.create(15,"unsorted");
LinkedList.display(head);
int val = 8;
ListNode result = removeElements(head,val);
LinkedList.display(result);
}
}