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

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

ps:这一题感觉没什么技巧可言,选取一个头指针,一个当前指针,一个前向指针。简单的链表操作而已,代码如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode * root = head;
ListNode * prev = head;
ListNode * curr = head;
while (curr != NULL){
if (curr->val == val){
if (prev == curr)
root = prev = curr = curr->next;
else{
curr = curr->next;
prev->next = curr;
}
}else{
prev = curr;
curr = curr->next;
}
}
return root;
}
};

java版本如下所示:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
while(head != null && head.val == val){
ListNode helper = head;
head = head.next;
helper.next = null;
}
if(head == null)
return head;
ListNode p1 = head;
while(p1.next != null){
if(p1.next.val == val){
p1.next = p1.next.next;
}else
p1 = p1.next;
}
return head;
}
}

LeetCode OJ :Remove Linked List Elements (移除链表元素)的更多相关文章

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

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  2. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  3. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

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

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

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

  6. LeetCode 203. Remove Linked List Elements (移除链表中的项)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  7. 【leetcode】Remove Linked List Elements(easy)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. Java for LeetCode 203 Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  9. (easy)LeetCode 203.Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

随机推荐

  1. day14生成器

    生成器 我自己想写个可迭代的,——生成器生成器的本质就是迭代器因此生成器的所有好处都和迭代器一样但是生成器是我们自己写的python代码生成器的实现有两种方式:1.生成器函数2.生成器表达式 def ...

  2. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  3. Java并发之CyclicBarria的使用(二)

    Java并发之CyclicBarria的使用(二) 一.简介 之前借助于其他大神写过一篇关于CyclicBarria用法的博文,但是内心总是感觉丝丝的愧疚,因为笔者喜欢原创,而不喜欢去转载一些其他的文 ...

  4. ubuntu 16.04安装navicat for mysql

    下载地址:官网https://www.navicat.com/download 1.下载 navicat120_mysql_en_x64.tar.gz 文件  2.下载后移到/opt/下 3.解压ta ...

  5. javascript设计模式 - 解释器模式(interpreter)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 基于 CodeIgniter 的各类开源项目大全

    名称:STBlog 介绍:STBlog 是一套由CI中国社区驱动,基于Codeigniter MVC 框架编写的多权限博客系统,轻巧/快速/安全/易拓展/界面友好是它的最大特点. 官方:http:// ...

  7. 基于主从复制的Mysql双机热备+amoeba实现读写分离、均衡负载

    读写分离指的是客户只能在主服务器上写,只能在从服务器上读,当然了,这也是要看配置,你可以在主服务器配置读的功能,但是在从服务器上只能读不能写,因为从服务器是基于binlog对主服务器的复制,如果在从服 ...

  8. 使用display:table使两栏布局高度相等

    两栏布局大家应该经常用了,但是遇到坑爹的要两栏的高度对齐的话要怎么办呢? <!DOCTYPE html> <html> <head> <meta charse ...

  9. MYSQL 多实例运行

    1.创建数据文件 mkdir /var/lib/mysql_3307 mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql 2.给数据 ...

  10. tyvj 1067 合唱队形 dp LIS

    P1067 合唱队形 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2004 提高组 第三道 描述     N位同学站成一排,音乐老师要请其中的(N ...