zoukankan      html  css  js  c++  java
  • 【刷题-LeetCode】203. Remove Linked List Elements

    1. Remove Linked List Elements

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

    Example:

    Input:  1->2->6->3->4->5->6, val = 6
    Output: 1->2->3->4->5
    

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode() : val(0), next(nullptr) {}
     *     ListNode(int x) : val(x), next(nullptr) {}
     *     ListNode(int x, ListNode *next) : val(x), next(next) {}
     * };
     */
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            ListNode *dummy = new ListNode(0, head);
            ListNode *pre = dummy, *cur = head;
            while(cur){
                if(cur->val == val){
                    pre->next = cur->next;
                    cur = cur->next;
                }else{
                    pre = cur;
                    cur = cur->next;
                }
            }
            return dummy->next;
        }
    };
    
  • 相关阅读:
    JS和Flash相互调用
    xml的应用
    随机验证码
    模块 time
    第一天 注册成功
    我的第一篇博客
    git
    2018-02-27
    25
    建站之星
  • 原文地址:https://www.cnblogs.com/vinnson/p/13303584.html
Copyright © 2011-2022 走看看