Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- public class Solution {
- /**
- * @param head a ListNode
- * @param val an integer
- * @return a ListNode
- */
- public ListNode removeElements(ListNode head, int val) {
- // Write your code here
- // Write your code here
- while (head!=null){
- if (head.val == val) {
- head = head.next;
- }
- else break;
- }
- ListNode tmp = new ListNode(0);
- tmp.next = head;
- while (tmp.next != null && tmp.next.next!=null) {
- if (tmp.next.val == val) {
- tmp.next = tmp.next.next;
- }
- tmp = tmp.next;
- }
- if (tmp!=null){
- if (tmp.val==val){
- tmp=tmp.next;
- }
- }
- if (tmp.next!=null){
- if (tmp.next.val==val){
- tmp.next=null;
- }
- }
- return head;
- }
- }
- -------------------
- Runtime Error
- Total Runtime: 3540 ms
- 97% test cases passed.
- Input
- 5->6->6->null, 6
- Expected
- 5->null
- Error Message
- Exception in thread "main" java.lang.NullPointerException at Solution.removeElements(Solution.java:42) at Main.main(Main.java:13)
Advertisement
Add Comment
Please, Sign In to add comment