leetcode203. 移除链表元素
方法一(删除头结点时另做考虑)
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. 移除链表元素的更多相关文章
- [Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- Leecode刷题之旅-C语言/python-203移除链表元素
/* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...
- Java实现 LeetCode 203 移除链表元素
203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...
- [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)
题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...
- 【LeetCode】203.移除链表元素
203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- 力扣(LeetCode)移除链表元素 个人题解
删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和v ...
随机推荐
- ORB-SLAM2初步(源码逻辑分析)
今天主要是梳理一下ORB-SLAM2源码的逻辑关系,GitHub和泡泡机器人上有很好的注释版本(吴博),大神请(轻)板砖. 一.文件 如图所示,Examples里面存放的分别是基于单目.双目.RGBD ...
- Note | Ubuntu
目录 0. 教程 1. 安装 2. 系统 0. 教程 <Linux就该这么学>:https://www.cnblogs.com/RyanXing/p/9462850.html 1. 安装 ...
- Java 常用正则表达式搜集ing
MAC地址: ^[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{ ...
- Wireshark使用入门
目录 1. Wireshark介绍 1.1 客户端界面 1.2 Display Filter 的常用方法 1.3 界面上一些小TIPS 2. 使用Wireshark分析TCP三次握手过程 2.1 三次 ...
- 聊一下,前后分离后带来的跨域访问和cookie问题
在谈前后分离前,我们先看看什么是前后一体的.当我们用javaweb开发网站时,最终我们渲染的jsp或者springthymeleaf.我们的页面其实是WEB-INFO或者templates下.当用户请 ...
- mybatis报错:Invalid bound statement (not found)
mybatis报错:Invalid bound statement (not found)的原因很多,但是正如报错提示一样,找不到xml中的sql语句,报错的情况分为三种: 第一种:语法错误 Java ...
- 使用 jQuery.TypeAhead 让文本框自动完成 (四)(自定义模板)
项目地址:https://github.com/twitter/typeahead.js 直接贴代码了: @section headSection { <script type="te ...
- Web Api全局预防Xss攻击
本文转载自https://www.cnblogs.com/ruanyifeng/p/4739807.html.对第二种过滤方法的代码进行了一些修改和注释,记录一下免得以后忘了.已经测试过,应该可以直接 ...
- js、jquery、css属性及出错集合
*)注意使用jquery设置css的语法 css("propertyname","value");#单个时时逗号 css({"propertyname ...
- Python基础17
写出来的代码,若有部分不想运行,可注释掉. 看跑出来的结果,再加进来调试.