leetcode203
/**
* 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的更多相关文章
- Leetcode-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- [LeetCode203]Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 ...
- [Swift]LeetCode203. 移除链表元素 | Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...
- leetcode203. 移除链表元素
方法一(删除头结点时另做考虑) class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(head ...
- 十五 链表与递归,leetCode203题
两种方式: package com.lt.datastructure.LinkedList; /** * leetCode 203题 * /** * Definition for singly-lin ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
- LeetCode通关:听说链表是门槛,这就抬脚跨门而入
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master https://github.com/ch ...
- Leetcode刷题之链表增加头结点的前缀节点
链表之增加头结点的前缀节点 在许多链表题中往往需要在题目给的头结点之前增加一个前缀节点 通常在删除链表和头结点需要交换时需要用到这一操作 因为增加这个节点就避免了对删除头结点这种特殊情况的特殊处理 而 ...
随机推荐
- VS2019/VS2017安装源离线下载,更新,清理,企业版与论坛版重复下载
VS2019 安装器下载 https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel ...
- python学习笔记(五)---sublime text 多行代码注释快捷键
转载网址:https://blog.csdn.net/mycms5/article/details/70194045/ 多行选择后按下ctrl+/ 选择类 Ctrl+D 选中光标所占的文本,继续操作则 ...
- PHP与Java集成开发详解(一)
很久以前,有人从www上看到看到天空上一个很亮的亮点,它就是Java语言,与此同时,在另一个地方一位梦想家也看到了一个亮点,它就是PHP. 时间一天天过去,这两个亮点也变得越来越亮,很快,它们受到了编 ...
- hrbust 1621 迷宫问题II 广搜
题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...
- 【转】socket 通信简介
转自:http://blog.csdn.net/xiaoweige207/article/details/6211577/ “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是 ...
- Location对象的页面跳转方法介绍
JavaScript中使用location对象可以通过很多种方式改变浏览器的位置.最常用的方法应该是下面几种: demo.js 1 2 3 location.href = "http://w ...
- Jenkins无法读取覆盖率报告的解决方法
报错信息如下: log 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 coverage-report: [mkdir] Cre ...
- 『转』Bitdefender Internet Security 2013 – 免费1年
活动中可以选择获取Bitdefender Internet Security 2013+Bitdefender Mobile Security (手机版)各一年激活码申请地址:https://part ...
- Tensorflow 模型文件结构、模型中Tensor查看
tensorflow训练后保存的模型主要包含两部分,一是网络结构的定义(网络图),二是网络结构里的参数值. 1. .meta文件 .meta 文件以 "protocol buffer&qu ...
- How to convert a QString to unicode object in python 2?
How to convert a QString to unicode object in python 2? I had this problem to solve, and I tried to ...