/**
* 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. IOS-网络(发送JSON数据给服务器和多值参数)

    三步走: 1.使用POST请求 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Co ...

  2. bzoj2163

    题解: 拆点网络流 然后用总和-最大流 代码: #include<iostream> #include<cstring> #include<cstdio> #inc ...

  3. android 笔记一

    1.Fragment创建extents FragmentView rootView = inflater.inflate(R.layout.fragment_main, container, fals ...

  4. linux命令返回值的含义

    linux命令执行后无论成功与否都有一个返回值: 如果为 0,则表示命令执行成功,其它值则表示错误, 具体的错误码含义如下: "OS error code 1: Operation not ...

  5. threejs通过射线Ray获取指定的点

    例:获取cube方向上的面的中点坐标(该cube默认方向为(0,1,0)) (中心点向cube quaternion 方向上发射射线,与正前方的面相交的点即为目标点; 由于ray只支持box和face ...

  6. POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】

    LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...

  7. Objective-C教程备忘单

    终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...

  8. 利用Web Services开发分布式应用

    一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 We ...

  9. 【算法】通过TreeMap理解红黑树

    本文以Java TreeMap为例,从源代码层面,结合详细的图解,剥茧抽丝地讲解红黑树(Red-Black tree)的插入,删除以及由此产生的调整过程. 总体介绍 Java TreeMap实现了So ...

  10. 毕业了-java二叉树层次遍历算法

    /*************************************** * 时间:2017年6月23日 * author:lcy * 内容:二叉树的层次遍历 * 需要借助队列这个数据结构,直 ...