Have no clue why my program cannot accept


  • 0
    J
    	if(head == null) return null;
    	ListNode prv = head;
    	ListNode temp = head;
    	while(temp != null){
    		prv = temp;
    		if(temp.val == val){
    			prv.next = temp.next;
    		}
    		temp = temp.next;
    	}
        return head;

  • 1
    S
     // For the code you have written the placement of prv=temp step is in wrong place.   
     // if prv = temp then prv.next and temp.next are always equal . Improvement to your code will be  
     // while(temp != null){
     //          if(temp.val == val){
     //              prv.next = temp.next;
     //              temp = temp.next;
     //              continue;
     //           }
     //           prv = temp;
     //           temp = temp.next;
     // }
     // Following code will consider all the test cases.
    
    public ListNode removeElements(ListNode head, int val) {
                if(head==null)
                    return null;
                if(head.next==null){
                    if(head.val==val)
                        return null;
                    else
                        return head;
                }
                while(true){
                    if(head == null)
                        return null;
                    if(head.val==val)
                        head = head.next;
                    else
                        break;
                }
                ListNode prev = head;
                ListNode curr = head.next;
                while(curr!=null){
                    if(curr.val==val){
                        prev.next = curr.next;
                        curr = curr.next;
                        continue;
                    }
                    prev = curr;
                    curr = curr.next;
                }
                return head;
            }

  • 0
    L
    public class Solution {
        public ListNode removeElements(ListNode head, int val) {
            ListNode prev = null;
            ListNode curr = head;
            
            while(curr != null) {
                ListNode next = curr.next;
                
                if(curr.val == val) {
                    if(prev == null) {
                        head = next;
                        curr.next = null;
                    } else {
                        prev.next = curr.next;
                        curr.next = null;
                    }
                } else {
                    prev = curr;
                }
                
                curr = next;
            }
            
            return head;
        }
    }

  • 0
    L

    The prev value is wrong because it must start set to null. Then you need to check if you are removing the first element (prev == null) or another element in the middle or end.

    This line "prv.next = temp.next" is not doing anything because prev is equal to temp, then prev.next is equal to temp.next.


  • 0
    X

    thanks for your answer~~ because I meet with the same problem as the owner. I think the key point is I don't think more about the pre-condition or the special condition, such as: " [6,6,1,6] 6 " or " [1] 2 " ...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.