# Remove Linked List Elements

## 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):
def removeElements(self, head, val):
"""
: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

/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* @param head a ListNode
* @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会比较难处理。