# 203. Remove Linked List Elements 移除链表元素

@TOC

## # 题目描述

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

Example

``````Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
``````

## # 解题方法

### # 双指针

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
return null;
}
ListNode curr = pre.next;
while(curr != null){
if(curr.val == val){
pre.next = curr.next;
}else{
pre = curr;
}
curr = curr.next;
}
}
}
``````

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(-1)
pre = dummy
while cur:
if cur.val == val:
pre.next = cur.next
else:
pre = pre.next
cur = cur.next
return dummy.next
``````

### # 递归

Java代码如下。

``````public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
}
``````

Python代码如下：

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

class Solution(object):