## Question

### Problem Statement

Remove all elements from a linked list of integers that have value val.

#### Example

Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

## 題解

### Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
"""
:type val: int
:rtype: ListNode
"""
dummy = ListNode(0)
curr = dummy
while curr.next is not None:
if curr.next.val == val:
curr.next = curr.next.next
else:
curr = curr.next

return dummy.next


### Java

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param val an integer
* @return a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while (curr.next != null) {
if (curr.next.val == val) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}

return dummy.next;
}
}


### 源碼分析

while 循環中使用curr.next較爲方便，if 語句中比較時也使用curr.next.val也比較簡潔，如果使用curr會比較難處理。