203. 移除链表元素

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

解法:没什么好说的,直接删即可
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head!=null && head.val == val){
            head = head.next;
        }
        if(head == null){
            return head;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val == val){
                pre.next = cur.next;
                cur.next = null;
                cur = pre;
            }else{
                pre = pre.next;
            }
            cur = cur.next;
        }
        return head;
    }
}
原文地址:https://www.cnblogs.com/czsy/p/10963832.html