/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @param val int整型 
 * @return ListNode类
 */
#include <stdlib.h>
struct ListNode* deleteNode(struct ListNode* head, int val ) {
    // write code here
    if (head == NULL){
        return head;
    }
  //如果第一个元素为val,直接返回next,排除了只有一个元素或val不在链表情况
    if (head->val == val) {
        return head->next;
    }
   //ans指向链表开头不做修改
    struct ListNode* ans;
    ans = (struct ListNode*)malloc(sizeof(struct ListNode));
    ans->next = head;
  //pre修改链表
    struct ListNode* pre;
    pre = (struct ListNode*)malloc(sizeof(struct ListNode));
  //保存val的下个元素
    struct ListNode* temp;
    temp = (struct ListNode*)malloc(sizeof(struct ListNode));
    pre->next = head;
    while (head) {  
        if (head->val == val) {
            temp = head->next;
            pre->next = temp;
		  //确定val不在第一个元素
            return ans->next;
        }
        pre = head;
        head = head->next;
    }
  //val不在链表 head == null
    return head;
}