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* ...
随机推荐
- Backpack II
Description There are n items and a backpack with size m. Given array A representing the size of eac ...
- Centos7 minimal 安装npm
最小版本缺少很多源,需要手动去添加源 如何去判断yum中 有没有 npm 的源呢 yum list | grep npm 如果是这样的,就代表需要自己去添加 curl -sL -o /etc/yum. ...
- (二)IDEA使用,快捷键
idea的快捷键设置: idea支持使用其他开发工具的快捷键,可以在此设置: idea 默认的本身的快捷键: 常用快捷键 Ctrl + Alt + s 打开设置选项 Alt + Enter 修复提示 ...
- VS - Paginated
BootstrapPagination.cshtml @model PaginationModel <div class="pagination"> <ul> ...
- WAMP 403 Forbidden禁止访问,别的电脑访问不了;
直接上图: 1:修改httpd.conf; deny from all 改成------ allow from all 重启服务就好了: 2:如果搜不到deny from all 就按照下面的方法来 ...
- PHP-FPM参数详情
pid = run/php-fpm.pid #pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启 error_log = log/php-fpm.log #错误日志,默认在 ...
- Problem 2 线段树
$des$ 一个无限长的 01 序列,初始全为 0,每次选择一个区间 [l,r] 进行操作,有三种操作:1. l r 将 [l,r] 中所有元素变成 1.2. l r 将 [l,r] 中所有元素变成 ...
- LibreOJ #527. 「LibreOJ β Round #4」框架
二次联通门 : LibreOJ #527. 「LibreOJ β Round #4」框架 /* LibreOJ #527. 「LibreOJ β Round #4」框架 %% xxy dalao 对于 ...
- Luogu5591 小猪佩奇学数学 【单位根反演】
题目链接:洛谷 \[ Ans=\frac{1}{k}(\sum_{i=0}^n\binom{n}{i}p^ii-\sum_{i=0}^n\binom{n}{i}p^i(i \ \mathrm{mod} ...
- (33)Vue购物车
computed:{ lists(){ return this.$store.state.list }, }, 用v-model来双向绑定input控制checkbox是否选中 Vue中双向数据绑定是 ...