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. django Form表单的使用

    Form django表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm 关于django的表单系统,主要分两种 基于django.forms.Form ...

  2. Longest Palindromic Substring-Dynamic Programing

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. Analysi ...

  3. python sqlite

    1.导入Python SQLITE数据库模块 Python2.5之后,内置了SQLite3,成为了内置模块,这给我们省了安装的功夫,只需导入即可~ import sqlite3 2. 创建/打开数据库 ...

  4. Lambda加自定义比较器实现两个列表的合并

    一次项目有这样的需求,本地存储了json数据,可以转化为对应的List列表,现在需要更新,从服务器那里获取最新的数据更改.总的来说就是本地有个List表,如果数据需要更新,则会向服务器发送请求来获取需 ...

  5. websocket redis实现集群即时消息聊天

    websocket与redismq实现集群消息聊天 1.application.properties server.port=8081 #thymeleaf配置 #是否启用模板缓存. spring.t ...

  6. hadoop21---使用代理修改List,代理流程

    package cn.itcast_05_proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Met ...

  7. Spring 之通过 XML 装配 bean

    1.关于 使用传统标签还是 c- p- 命名空间定义的标签, 我的观点是能用  c- p- 命名空间定义的标签 就不用 传统标签(这样会比较简洁... 2.强依赖使用构造器注入,可选性依赖使用属性注入 ...

  8. level-13

    如何调试IE浏览器 1.打开IE浏览器,F12打开开发者模式.(针对IE7及以上) 2.针对IE6浏览器.使用虚拟机或者用ietester 什么是CSS hack?在 CSS 和 HTML里如何写 h ...

  9. MySQL-5.7密码策略及用户资源限制

    1.密码策略 在mysql 5.6对密码的强度进行了加强,推出了validate_password 插件.支持密码的强度要求. (1)安装插件 [root@localhost ~]# ll /usr/ ...

  10. JavaEE之Junit单元测试

    1编写测试类,简单理解Junit可以部分用于取代java的main方法 2在测试类方法上添加注解 @Test 3 @Test修饰的方法要求:public void 方法名() {…} ,方法名自定义建 ...