Remove all elements from a linked list of integers that have value val. here
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
public ListNode removeElements(ListNode head, int val) {
while (head != null && head.val == val)
head = head.next;
ListNode prev = null, curr = head;
while (curr != null) {
if (curr.val == val) {
prev.next = curr.next;
} else
prev = curr;
curr = curr.next;
}
return head;
}
Definition for singly-linked list
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
Compile with javac Solution.java
and run with java Solution
.
This is only for discussion and communication. Please don't use this for submission of assignments.