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刷题之链表增加头结点的前缀节点
链表之增加头结点的前缀节点 在许多链表题中往往需要在题目给的头结点之前增加一个前缀节点 通常在删除链表和头结点需要交换时需要用到这一操作 因为增加这个节点就避免了对删除头结点这种特殊情况的特殊处理 而 ...
随机推荐
- Html中的表格
表格由<table>标签来定义.每个表格均有若干行(由<tr> 标签定义),每行被分割为若干单元格(由<td>标签定义). 字母 td 指表格数据(table da ...
- bzoj2325
题解: 树链剖分 和普通的树链剖分不一样,这里的线段树不只是要记录x-y的和 而是要记录x左到y左,x左到y右,x右到y左,x右到y右 然后就可以了 代码: #include<bits/stdc ...
- Git 学习之 Git Basics
最近在用git,但git学习曲线实在是有点高. 好在找到一个文档 https://www.atlassian.com/git/tutorial/,以下就是学习笔记吧! git init git ini ...
- Go安装一些第三方库
原文链接:https://javasgl.github.io/go-get-golang-x-packages/ 侵权联系删除! go在go get 一些 package时候的会由于众所周知的原因而无 ...
- 配置java环境变量,实现一条命令自由切java7 或java8
在多个java编译环境中,有时需要java 7,有时又需要java 8,怎么配置java 环境,可以快速自动切换呢?下面用mac演示在 /etc/bashrc 中配置的环境变量 # 设置 JDK ex ...
- npm 与 package.json 快速入门
npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本文你将了解: 什么是 npm 安装 npm 更新 npm ...
- Swift UIImageView和UISlider组合
/***************火焰图片Demo************start*******/ var imgView: UIImageView? override func viewDidLoa ...
- UITableView-(单元格的自定义方法)
//contentView //行内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS ...
- maven打jar到私服
<dependency> <groupId>fakepath</groupId> <artifactId>wcs-java-sdk</artifa ...
- PTHREAD的WINDOWS开发包
PTHREAD的WINDOWS开发包 网站地址是http://sourceware.org/pthreads-win32/