zoukankan      html  css  js  c++  java
  • leetcode 203. Remove Linked List Elements

    题目描述:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if(head == NULL){
                return NULL;
            }
            ListNode *newHead = new ListNode(-1);
            newHead->next = head;
            ListNode *cur = head;
            ListNode *pre = newHead;
            while(cur!=NULL){
                if(cur->val == val){
                    cur = cur->next;
                    pre->next = cur;
                }
                else{
                    pre = cur;
                    cur = cur->next;
                }
            }
            return newHead->next;
        }
    };
  • 相关阅读:
    Title
    Title
    Title
    Title
    Python生成随机验证码
    Time模块和datetime模块
    For循环的实质
    Python函数
    集合set
    字符串内置方法的使用
  • 原文地址:https://www.cnblogs.com/strongYaYa/p/6932342.html
Copyright © 2011-2022 走看看