203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】
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.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
ListNode cur = head;
while (cur.next != null) {
if (cur.next.val == val) cur.next = cur.next.next;
else cur = cur.next;
}
return head.val == val ? head.next : head;
} }
【递归】
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
} }
203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】的更多相关文章
- LeetCode 203. Remove Linked List Elements (移除链表中的项)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode--LinkedList--203. Remove Linked List Elements(Easy)
203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 【一天一道Leetcode】#203.Remove Linked List Elements
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
随机推荐
- 51Nod 1133 不重叠的线段 | 典型贪心
Input示例 3 1 5 2 3 3 6 Output示例 2 题意:给出n条一维线段,求不重合的最多线段数. 解析:这个是典型的贪心算法的区间问题. 贪心策略:每次取尽可能短的区间,而且保证相互之 ...
- 【Foreign】咏叹 [模拟退火]
咏叹 Time Limit: 100 Sec Memory Limit: 256 MB Description 有n根木棍,第i根长度为ai.你要贴着墙围出一个矩形区域,木棍围成的矩形边缘必须平行或 ...
- Mac 上真机调试cocos2d-x-3.16的test程序
文章比较长,一个算是新手又不是新手的程序员的解决过程. 一 xcode中打开项目 首先,下载完成cocos2d-x-3.16之后,解压,然后在根目录build目录下双击cocos2d_tests.xc ...
- 对vue中 默认的 config/index.js:配置的详细理解 -【以及webpack配置的理解】-config配置的目的都是为了服务webpack的配置,给不同的编译条件提供配置
当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面 (环境变量及其基本变量的配置) var path = require('path') ...
- jeecg3.7中弹出窗操作标签dgOpenOpt的用法
1.基本参数 参数名 描述 url 弹出页面地址 title ...
- HTTP响应码摘自apach官网
HTTP状态列表 响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示: 1xx:信息,请求收到,继续处理 2xx:成功,行为被成功地接 ...
- Keil MDK 5.14 仿真时System Viewer菜单显示空白和Peripherals菜单无外设寄存器
keil mdk5.14新建工程进行仿真时,进入Debug环境发现System Viewer菜单显示空白,Peripherals菜单没有外设寄存器.如图1和图2所示.打开Oprons for Targ ...
- android CVE 漏洞汇总
arm exploits 技术教程: Learning Pentesting for Android Devices CVE-2015-1530 ,CVE-2015-1474 两个android整数溢 ...
- 【ZJOI2016】大森林
这题理论上可以用ETT,但是用LCT建虚点可以解决这个问题. 对于最晚的操作1建立一个虚点,然后把操作0挂上去. #include<bits/stdc++.h> ; using names ...
- 转载: GIt远程操作详解
Git远程操作详解 作者: 阮一峰 日期: 2014年6月12日 Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介 ...