public static ListNode removeElements(ListNode head, int val) {
    ListNode p = head;
    ListNode q = null;
    while (null != p && val == p.val) {
      p = p.next;
      head = head.next;
    }
    if (null == head) return null; 
    p = head.next;
    q = head;
    while (null != p) {
      if (val == p.val) {
        q.next = p.next;
        p = p.next;
      } else {
        q = q.next;
        p = p.next;
      }
    }
    return head;
  }