```
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = dummyHead;
ListNode node=head;
while (node!=null) {
if(node.val==val) {
node=node.next;
} else {
p.next=node;
p=p.next;
node=node.next;
}
}
return dummyHead.next;
}
```

At first glance on this problem I think it can be easily solved, then I found my solution is wrong. but when I change it as following it works well. I am confused about why the first answer is wrong?

```
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = dummyHead;
ListNode node=head;
while (node!=null) {
if(node.val==val) {
p.next=node.next;
node=node.next;
} else {
//p.next=node;
p=p.next;
node=node.next;
}
}
return dummyHead.next;
}
```