Leetcode 203 Remove Linked List Elements 链表

时间:2023-02-03 18:02:47

去掉链表中相应的元素值

 /**
* 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) return NULL;
ListNode* now = head;
for(;now->next ;){
if(now->next->val == val){
ListNode* next = now->next;
now->next = next->next;
delete next;
}
else now = now->next;
}
if(head->val == val) {
ListNode* t = head;
head = head->next;
delete t;
}
return head;
}
};

Leetcode 203 Remove Linked List Elements 链表的更多相关文章

  1. [leetcode]203. Remove Linked List Elements链表中删除节点

    这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) { //超前节点 ListNode pre = ...

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

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

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

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

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

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

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

  6. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

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

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

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

  9. [LeetCode] 203. Remove Linked List Elements 解题思路

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

随机推荐

  1. Mina入门教程(二)----Spring4 集成Mina

    在spring和mina集成的时候,要十分注意一个问题:版本. 这是一个非常严重的问题,mina官网的demo没错,网上很多网友总结的代码也是对的,但是很多人将mina集成到spring中的时候,总是 ...

  2. java: R文件重复

    导入eclipse工程到intellij里面, 然后出现各种错误, xxx.R文件重复, 各种资源id在R文件中找不到, 后来发现是intellij默认将整个项目以及gen文件夹作为源代码目录了, 在 ...

  3. 设计模式 之 装饰者(Decorator)模式

    装饰者模式(Decorator):动态地为一个对象添加一些额外的职责,若要扩展一个对象的功能,装饰者提供了比继承更有弹性的替代方案. 结构图: 抽象构件类(Component):给出一个抽象的接口,用 ...

  4. IDisplayTransformation

    IDisplayTransformation Bounds Full extent in world coordinates. The Bounds property controls the ful ...

  5. leetcode 118 Pascal's Triangle ----- java

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. Codeforces Round #328 div2

    Problem_A(592A): 题意: 在一个8*8的棋盘上有黑白两种棋子,'W'代表白色,'B'代表黑色. 玩家A执白子,先走. 白子只能向上走,黑子只能向下走.如果有障碍物则不能走, 比如白色的 ...

  7. Mybatis中 collection 和 association 的区别

    public class A{ private B b1; private List<B> b2;} 在映射b1属性时用association标签,(一对一的关系) 映射b2时用colle ...

  8. Redux与它的中间件:redux-thunk,redux-actions,redux-promise,redux-saga

    序言 这里要讲的就是一个Redux在React中的应用问题,讲一讲Redux,react-redux,redux-thunk,redux-actions,redux-promise,redux-sag ...

  9. struct 与 class 的区别

    C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能. struct能包含成员函数吗? 能! struct能继承吗? 能!! s ...

  10. 如何查看thinkphp版本号?

    我们有时不知道thinkphp版本号,怎么查看呢?很简单,tp5版本在/thinkphp/base.php文件中就可以查看define('THINK_VERSION', '5.0.15');如下图所示 ...