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

题目要求:

删除链表中包含val的元素结点

解题思路:

重点在于找到第一个非val的头结点,然后遍历链表,依次删除值为val的结点,最后返回头结点

方法:

1、常规方法:

找到第一个非val的头结点,如果头结点非NULL,遍历链表,依次删除值为val的结点,最后返回头结点。

2、递归方法:

代码:

/**
* 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) {
while(head!=NULL && head->val==val)
head=head->next;
if(head==NULL)
return head;
// At least one node that does not contain val
ListNode* cur;
cur=head;
while(cur->next!=NULL){
if(cur->next->val==val)
cur->next=cur->next->next;
else
cur=cur->next;
}
return head;
}
};
/**
* 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) {
if(head && head->val==val)
head=removeElements(head->next,val);
if(head && head->next)
head->next=removeElements(head->next,val);
return head;
}
};

(LeetCode 203)Remove Linked List Elements的更多相关文章

  1. LeetCode算法题-Remove Linked List Elements(Java实现)

    这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> ...

  2. [LC]203题 Remove Linked List Elements (移除链表元素)(链表)

    ①英文题目 Remove all elements from a linked list of integers that have value val. Example: Input: 1-> ...

  3. (LinkedList) Remove Linked List Elements

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

  4. LeetCode OJ :Remove Linked List Elements (移除链表元素)

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

  5. (LeetCode 83)Remove Duplicates from Sorted Lists

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

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

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

  8. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

  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. BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流

    https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...

  2. hdu 4451 Dressing 排列组合/水题

    Dressing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. opencv 利用Haar 人脸识别

    #include <opencv2/opencv.hpp> #include <cstdio> #include <cstdlib> #include <io ...

  4. Google Code Jam 2009 Qualification Round Problem B. Watersheds

    https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...

  5. Maven使用(转)

    说明:文章转自http://www.cnblogs.com/JeffreySun/archive/2013/03/14/2960573.html 创建project 先去官方网站下载一个最新版本htt ...

  6. PHP中的 变量 与 常量 详解

    几乎所有的编程语言都会涉及到变量和常量这两个概念,PHP也不例外.本节将介绍PHP语言中的变量和常量的应用方法. 一.什么是变量和常量 在程序执行的过程中,变量存储的值可以随时改变,而常量存储的值是不 ...

  7. 编译Opencv的GPU,利用CUDA加速

    首先检查自己的机器是否支持,否则都是白搭(仅仅有NVIDIA的显卡才支持.可在设备管理器中查看) 假设不用GPU.能够直接官网下载预编译好的库 环境: 1 VS2013 2 Opencv2.4.9 3 ...

  8. MVC实现文件下载

    □ 思路 点击一个链接,把该文件的Id传递给控制器方法,遍历文件夹所有文件,根据ID找到对应文件,并返回FileResult类型. 与文件相关的Model: namespace MvcApplicat ...

  9. mac 拷贝文件时报错 8060 解决方案

    解决如下: 即某文件夹下出现多重子目录,级数很多,删除多余的子文件夹即可. 至于如何产生的,有人说是xcode升级导致,不过没有见证 .我的不属于这类情况的. (参见:http://macosx.co ...

  10. Unity中一键创建常用文件夹

    Unity中一键创建常用文件夹 说明 项目测试版本Unity5.3. 这个一个小工具:功能非常简单,就是一键给新建工程添加所有文件夹.到此结束. 但是具体咋操作呢? 与把大象装进冰箱一样,三步,下载代 ...