方法一(删除头结点时另做考虑)

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) { if(head!=NULL && head->val==val)
{
head=head->next;
}
if(head==NULL) return NULL;
//处理第一位是val的情况
while(head->val==val)
{
head=head->next;
if(head==NULL) return NULL;
}
ListNode *curr=head->next;
ListNode *pre=head;
while(curr!=NULL)
{ if(curr->val==val)
{
pre->next=curr->next;
}
else
pre=curr;
curr=curr->next;
}
return head; }
};

方法二(添加一个虚拟头结点)

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) { ListNode* vir=new ListNode(-);
vir->next=head;
ListNode *pre=vir;
while(pre->next!=NULL)
{ if(pre->next->val==val)
{
pre->next=pre->next->next;
}
else
pre=pre->next;
}
return vir->next; }
};

方法三(递归)

class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
if(head.val==val){
return head.next;
}else{
return head;
}
}
}

leetcode203. 移除链表元素的更多相关文章

  1. [Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...

  2. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  3. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

  4. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  5. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  6. 【LeetCode】203.移除链表元素

    203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...

  7. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  9. 力扣(LeetCode)移除链表元素 个人题解

    删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和v ...

随机推荐

  1. 在线程内关闭thread handle,导致server crash

    很多年以前了,那时tester发现一个server crash,通过测试pager功能很多次,可以把server搞崩溃. 一般来说,能再现的bug都不难改,不过这次因为要跑很多次test,才能再现cr ...

  2. Codeforces Round #554 (Div. 2) C 数论

    https://codeforces.com/contest/1152/problem/C 题意 a和b,找到k,使得lcm(a+k,b+k)最小(a,b:1e9) 题解 设gcd=gcd(a+k,b ...

  3. 封装cookie的设置和获取

    cookie的设置 function setCookie(key,value,options){ options=options||{}; var time=""; if(opti ...

  4. VMware虚拟机安装Centos7后设置静态ip

    VMware虚拟机安装Centos7后设置静态ip 1. 先设置虚拟机的网络,打开虚拟网络编辑器: 2. 选择Vm8 234都要勾选 3. 打开NAT设置,看到123,待会要用. 4. 打开虚拟机服务 ...

  5. 项目倒入maven 遇到的问题只有 main 了

    归根结底是倒入错了: (1)首先  view->Toolbar; (2) 点击 File==>project structure 然后:在 project settings中点击 modu ...

  6. nexus搭建maven仓库管理

    Linux搭建nexus仓库 1.安装jdk 1.1 获取安装包,解压到指定目录: 1 tar xf jdk.tar.gz -C /opt/export 1.2 配置环境变量: 1 # vim /et ...

  7. Azure Automation (7) 执行Azure SQL Job

    <Windows Azure Platform 系列文章目录> 之前Automation介绍的内容,是在ASM模式下自动化开关机. 本章将介绍如何在Automation中,设置开关机脚本, ...

  8. 解决python3.7 ModuleNotFoundError: No module named bz2

    解决: ModuleNotFoundError: No module named  bz2 ModuleNotFoundError: No module named '_lzma' 1.在操作系统中安 ...

  9. python3爬虫筛选所需要数据

    第一次使用博客园,也是第一篇文章,让我们一起开启学习之旅吧!! 昨天在为某授权系统做安全性测试的时候,可以未授权访问系统的用户登陆统计记录.由此想整理出部分用户名,作为暴力破解的用户名,检查是否存在用 ...

  10. date——系统时间的命令

    这是一个可以用各种姿势获得各种时间的命令.最近在写自动化定时脚本时学了一下. 参考:https://www.cnblogs.com/ginvip/p/6357378.html 比如: 利用cronta ...