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刷题之链表增加头结点的前缀节点
链表之增加头结点的前缀节点 在许多链表题中往往需要在题目给的头结点之前增加一个前缀节点 通常在删除链表和头结点需要交换时需要用到这一操作 因为增加这个节点就避免了对删除头结点这种特殊情况的特殊处理 而 ...
随机推荐
- Android------视频播放器(包含全屏播放,快退,快进,腾讯新闻的列表播放等)
前段时间做了一个新闻APP,涉及到了列表视频播放,和腾讯新闻APP差不多,总结了一下代码,写了一个Demo来分享给大家. 用了 TabLayout+RecylerView+自定义视频控件 完成的 ...
- 微信小程序封装storage(含错误处理)
这次给你们安利的是微信小程序封装storage,先说下微信官方的 wx.getStorage({ key:"", success: function (res) { }, fail ...
- 设置mysql的字符集
ALTER DATABASE test DEFAULT CHARACTER SET utf8; show variables like 'char%';
- centos7下的防火墙
在cengos7下的防火墙是交给systemctl来管理服务和程序,包括了service和chkconfig. 查看firewalld.service 是否启动 systemctl stop fire ...
- STL标准库-hash
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 hash的结构图如下图 oject通过hashfunc转换成hashcode然后插入到相应篮子中 hash同rb_tree是一种 ...
- React Native自适应设备宽度解决方案
px:设备实际像素单位 dp/pt:逻辑像素单位(IOS的尺寸单位为pt,Android的尺寸单位为dp) 在设计和开发过程中,应该尽量使用逻辑像素尺寸来思考界面. UI 给默认 640 的图,采用 ...
- [转] .net软件反编译笔记
原文地址:http://blog.csdn.net/three_bird/article/details/51433734 在软件的破解及源码获取及重新编译的道路上会遇到一些问题,书此备查. 大名鼎鼎 ...
- nginx默认的配置文件详解
# For more information on configuration, see:# * Official English Documentation: http://nginx.org/en ...
- PyAlgoTrade Hello World 第一个程序(一)
本教程的目标是快速介绍PyAlgoTrade.PyAlgoTrade的目标是帮助您实现股票交易策略.假设您有一个交易策略的想法,并且您希望使用历史数据进行评估,并查看其行为方式,那么PyAlgoTra ...
- 使TextView里面的文字滚动起来
要使TextView里面的内容滚动起来,那么就要配置好几个参数, 1.focusable来获取焦点. 2.ellipsize来获得滚动的方法. 3.focusableInTouchMode来获取触摸方 ...