My c++ solution using recursion and reference


  • 0

    the trick is using reference (ListNode* &L)

    class Solution {
    public:
    	void del_x(ListNode* &L,int x){
    		if(L == NULL) return;
    		if(L->val == x){
    			ListNode *p = L;
    			L = L->next;
    			delete p;
    			del_x(L,x);	
    		}
    		else
    			del_x(L->next,x);	
    	}
        ListNode* removeElements(ListNode* head, int val) {
            del_x(head,val);
            return head;  
        }
    };

Log in to reply
 

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