My c code use 12ms


  • 0
    G
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* removeElements(struct ListNode* head, int val) {
    		if (head == NULL)return head;
    		struct ListNode* my_head = (struct ListNode*)malloc(sizeof(struct ListNode));
    		my_head->next = head;
    		struct ListNode* pre_head = my_head;
    		while (head)
    		{
    			if (head->val == val)
    			{
    				pre_head->next = head->next;
    				free(head);
    				head = pre_head->next;
    			}
    			else
    			{
    				pre_head = head;
    				head = head->next;
    			}
    		}
    		head = my_head->next;
    		free(my_head);
    		return head;
    }

Log in to reply
 

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