203 Remove Linked List Elements 删除链表中的元素

删除链表中等于给定值 val 的所有元素。
示例
给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
返回: 1 --> 2 --> 3 --> 4 --> 5

详见:https://leetcode.com/problems/remove-linked-list-elements/description/

Java实现:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null){
            return null;
        }
        ListNode first=new ListNode(-1);
        first.next=head;
        ListNode cur=first;
        while(cur.next!=null){
            if(cur.next.val==val){
                cur.next=cur.next.next;
            }else{
                cur=cur.next;
            }
        }
        return first.next;
    }
}
原文地址:https://www.cnblogs.com/xidian2014/p/8746082.html