public static ListNode removeElements(ListNode head, int val) { while (head != null && head.val == val) head = head.next; ListNode p = head; while (head != null && head.next != null) { while (head.next != null && head.next.val == val) { head.next = head.next.next; } head = head.next; } return p; }
public static void main(String a[]) { ListNode node = new ListNode(1); node.print(); node = removeElements(node, 1); node.print(); }