LeetCode_203. Remove Linked List Elements
203. Remove Linked List Elements
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
package leetcode.easy; /**
* Definition for singly-linked list. public class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; } }
*/
public class RemoveLinkedListElements {
private static void print(ListNode l) {
if (l == null) {
return;
}
while (l != null) {
System.out.print(l.val);
if (l.next != null) {
System.out.print("->");
}
l = l.next;
}
System.out.println();
} public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
if (head.val == val) {
return removeElements(head.next, val);
} else {
ListNode p = head;
while (p.next != null) {
if (p.next.val == val) {
p.next = p.next.next;
} else {
p = p.next;
}
}
return head;
}
} @org.junit.Test
public void test() {
ListNode ln1 = new ListNode(1);
ListNode ln2 = new ListNode(2);
ListNode ln3 = new ListNode(6);
ListNode ln4 = new ListNode(3);
ListNode ln5 = new ListNode(4);
ListNode ln6 = new ListNode(5);
ListNode ln7 = new ListNode(6);
ln1.next = ln2;
ln2.next = ln3;
ln3.next = ln4;
ln4.next = ln5;
ln5.next = ln6;
ln6.next = ln7;
ln7.next = null;
print(ln1);
print(removeElements(ln1, 6));
}
}
LeetCode_203. Remove Linked List Elements的更多相关文章
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- Leetcode-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- 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题要求 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
- LeetCode--LinkedList--203. Remove Linked List Elements(Easy)
203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
随机推荐
- pycharm flask debug调试接口
pycharm中对某接口调试,使用print打印日志太麻烦,可以通过debug模式来调试 一.首先开启flask的debug开关 编辑configurations 勾选FLASK_DEBUG选项 已d ...
- SpringBoot官方文档学习(一)SpringApplication
Springboot通过main方法启动,在许多情况下,委派给静态SpringApplication.run方法: public static void main(String[] args) { S ...
- 【爬虫】把抓到数据存起来——爬虫绝配mongodb
[爬虫]把抓到数据存起来——爬虫绝配mongodb 视频地址 抓取数据的方法,前面的课程该讲的都已经讲了,爬取下来数据只是第一步,第二步就是要先存起来.我们最容易想到的就是存文件里喽,python写文 ...
- UVA 11468 Substring (记忆化搜索 + AC自动鸡)
传送门 题意: 给你K个模式串, 然后,再给你 n 个字符, 和它们出现的概率 p[ i ], 模式串肯定由给定的字符组成. 且所有字符,要么是数字,要么是大小写字母. 问你生成一个长度为L的串,不包 ...
- Springboot如何优雅的解决ajax+自定义headers的跨域请求[转]
1.什么是跨域 由于浏览器同源策略(同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略.所谓同源是指,域名,协议,端口相同.),凡是发 ...
- codevs1504愚蠢的组合数 / RQNOJ愚蠢的组合数
1504 愚蠢的组合数 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 最近老师教了狗狗怎么算组合数,狗狗又 ...
- word 新建一行文字不能左对齐
- Java 使用Jedis和RedisTemplate操作Redis缓存(SpringBoot)
package com.example.redis.controller; import com.example.redis.entity.User; import com.example.redis ...
- Shell编程——脚本编写思路与过程
Linux系统Shell编程——脚本编写思路与过程 “ 前段时间有小伙伴问我一些问题,涉及到shell脚本的编写问题,事后,我深入思考了下,实际生产环境的确也会经常用到,因此如何写这个脚本?它的思路在 ...
- IDEA控制台乱码终极解决方案
1. 问题描述 由于本机的IDEA 2019.1出现了无法连接插件商店和Spring Boot模板的问题,就重装了了最新的IDEA 2019.2.4版本,使用了一段时间以后,没有改任何的配置,控制台的 ...