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

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. strcspn()函数

    函数描述: 检索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符. 函数声明: #include<string.h> size_t strcspn(const char ...

  2. Kubernetes 遇到的问题

    Kubernetes 遇到的问题 k8s 版本 k8s v1.13.5 docker 18.09.2 问题一 kubelet 报错 May 22 10:14:42 test-ops-k8s-03 ku ...

  3. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  4. redis之主从同步

    很多企业都没有使用到 Redis 的集群,但是至少都做了主从.有了主从,当 master 挂掉的时候,运维让从库过来接管,服务就可以继续,否则 master 需要经过数据恢复和重启的过程,这就可能会拖 ...

  5. .net怎么使用Swagger

    目录导航 一.安装 二.配置 三.调用 四.错误记录 一.安装 新建一个没有身份验证的mvc项目 - SwaggerMvc5Demo,然后添加一个名为Remote(自定义)且包含基础读写(不想手写)的 ...

  6. EF CodeFirst 使用T4模板

    实用等级:★★★★★ 首先,定义一个接口,代表一个领域实体.在定义一个实体集成这个接口,面向接口编程的各种好处就不提了. /// <summary> /// 代表一个领域实体 /// &l ...

  7. PhantomJS简单使用

    PhantomJS下载地址:   http://phantomjs.org/download.html 简单使用: from selenium import webdriver # 要想调用键盘按键操 ...

  8. JMeter之Http协议接口性能测试--基础

    一.不同角色眼中的接口 1.1,开发人员眼中的接口    1.2,测试人员眼中的接口 二.Http协议基本介绍 2.1,常见的接口协议 1.:2. :3. :4.:5.: 6. 2.2,Http协议栈 ...

  9. jsonHelper帮助类

    使用前,需引用开源项目类using Newtonsoft.Json 链接:https://pan.baidu.com/s/1htK784XyRCl2XaGGM7RtEg 提取码:gs2n using ...

  10. Java中级—JSP九大内置对象和动作

    一.内置对象 在JSP中,内置对象又称为隐含对象,是指在不声明和创建的情况下就可以被使用的一些成员变量.JSP一共提供有9个内置对象,分别是request(响应对象).pageContext(页面上下 ...