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
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的更多相关文章

  1. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  2. Leetcode-203 Remove Linked List Elements

    #203.   Remove Linked List Elements Remove all elements from a linked list of integers that have val ...

  3. 【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 ...

  4. 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题要求 ...

  5. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. LeetCode--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

  9. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

随机推荐

  1. dosbox+masm5.0编译汇编文件

    在去年写过如何bc3.1编译ucos,不过现在很少去用到,但是那是用dosbox也是懵懵懂懂的,参见https://blog.csdn.net/liming0931/article/details/8 ...

  2. python字符转数字

    print(float("260.01478420521632365364543423")) 260.0147842052163

  3. Error Permission denied when running brew cleanup

    Error Permission denied when running brew cleanup When I try to run `brew cleanup` I get: Warning: S ...

  4. C#实现上传/下载Excel文档

    要求 环境信息:WIN2008SERVER  开发工具:VS2015 开发语言:C# 要求: 1.点击同步数据后接口获取数据展示页面同时过滤无效数据并写入数据库,数据可导出Excel并支持分类导出 2 ...

  5. (2)React的开发

    实例: import React from 'react'; class TodoList extends React.Component { constructor(props){ super(pr ...

  6. linux protobuf 测试官方例子遇到报错及解决办法。

    测试例子时出现报错如下,在最下面会写出安装流程. -------------------------------------报错----1------------------------------- ...

  7. deepin 深度Linux系统 15.11 链接蓝牙鼠标问题

    不知道为毛就是搜索不到,好吧只能用老方法,那就是不使用deepin系统自带的面板进行管理 用下面的命令进行安装配置即可 sudo apt install bluetooth blueman bluem ...

  8. 第2组 团队Git现场编程实战

    目录 组员职责分工(1 2分) github 的提交日志截图(2 1分) 程序运行截图(3 3分) 程序运行环境(4 1分) GUI界面(5 5分) 基础功能实现(6 10分) 鼓励有想法且有用的功能 ...

  9. java代码拼写sql

    java后台    String zffwdm=map.get("zffwmc")==null?"":map.get("zffwmc"); ...

  10. HTML Entity

    1.1 介绍 在编写HTML页面时,需要用到"<".">"."空格"等符号,直接输入这些符号时,会错误的把它们与标记混在一起,非 ...