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

https://leetcode.com/problems/remove-linked-list-elements/#/description

leetcode203的更多相关文章

  1. Leetcode-203 Remove Linked List Elements

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

  2. [LeetCode203]Remove Linked List Elements

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

  3. [Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements

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

  4. leetcode203. 移除链表元素

    方法一(删除头结点时另做考虑) class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(head ...

  5. 十五 链表与递归,leetCode203题

    两种方式: package com.lt.datastructure.LinkedList; /** * leetCode 203题 * /** * Definition for singly-lin ...

  6. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  7. LeetCode通关:听说链表是门槛,这就抬脚跨门而入

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master       https://github.com/ch ...

  8. Leetcode刷题之链表增加头结点的前缀节点

    链表之增加头结点的前缀节点 在许多链表题中往往需要在题目给的头结点之前增加一个前缀节点 通常在删除链表和头结点需要交换时需要用到这一操作 因为增加这个节点就避免了对删除头结点这种特殊情况的特殊处理 而 ...

随机推荐

  1. bzoj-5049-线段树

    5039: [Jsoi2014]序列维护 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 323  Solved: 193[Submit][Status ...

  2. voj 1754 spfa

    最优贸易 最优贸易 描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 ...

  3. mysql插入中文数据报错 java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column

    1.我们创建数据库的时候没有更改数据库的字符集为utf8. 在mysql工具中,右击数据库,->"改变数据库",->选择“基字符集”为utf-8; 2,数据库中表的字符 ...

  4. poj1191 棋盘分割。 dp

    连接:http://poj.org/problem?id=1191 思路:额,其实就是直接搞记录一下就可以了. #include <stdio.h> #include <string ...

  5. 【Python】operator 模块简单介绍

    简单介绍几个常用的函数,其他的请参考文档. operator.concat(a, b) **operator.__concat__(a, b)** 对于 a.b序列,返回 a + b(列表合并) -- ...

  6. 发现TypeScript中同名interface接口会自动合并的特性

    今天在学习怎么用TypeScript给jQuery写扩展插件时发现一个很有趣的事情

  7. 《转》快速导出SSRS之RDL文件

    select name,[path],cast(cast(content AS varbinary(max)) as xml) as RDLDef from dbo.[Catalog] where t ...

  8. layui table 数据表格 隐藏列

    现在国内的模板,也就layui一家独大了,其中的数据表格功能强大,但我不会用python或者django拼接json,输出发送给数据表格,那只好用笨办法,循环遍历吧. 数据表格中保留id列,是为了编辑 ...

  9. openGL之着色器程序的使用

    #define GLEW_STATIC #include <GL\glew.h> #include <GLFW\glfw3.h> #include<iostream> ...

  10. Winform创建解决方案

    Winform的开发工具可以使用VS2005---VS2013,版本在不断升级,VS的功能也越来越强大.本系列文章采用VS2012(以后全称VS)演示. 窗体是winform开发的基础,需要掌握窗体的 ...