Java [Leetcode 203]Remove Linked List Elements
题目描述:
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
解题思路:
链表操作。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode res = new ListNode(0);
res.next = head;
ListNode point = head, pre = res; while(point != null) {
if(point.val == val) {
if(point.next != null) {
point.val = point.next.val;
point.next = point.next.next;
} else {
point = null;
pre.next = null;
}
} else {
point = point.next;
pre = pre.next;
}
}
return res.next;
}
}
Java [Leetcode 203]Remove Linked List Elements的更多相关文章
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- Java for LeetCode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- LeetCode 203. Remove Linked List Elements (移除链表中的项)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LeetCode] 203. Remove Linked List Elements 解题思路
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Leetcode 203 Remove Linked List Elements 链表
去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...
- [leetcode]203. Remove Linked List Elements链表中删除节点
这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) { //超前节点 ListNode pre = ...
随机推荐
- 在线最优化求解(Online Optimization)之三:FOBOS
在线最优化求解(Online Optimization)之三:FOBOS FOBOS (Forward-Backward Splitting)是由John Duchi和Yoram Singer提出的[ ...
- Ignore files which are already versioned
If you accidentally added some files which should have been ignored, how do you get them out of vers ...
- 【redis】01Redis的介绍与安装部署
单元目标: 1.NoSQL介绍 2.Redis的介绍 3.Redis适用场合 4.Redis的安装与部署 5.Redis的数据类型 6.Redis的常用命令 7.Redis的高级应用 通过 ...
- poj 2253 Frogger (最短路变种,连通图的最长边)
题目 这里的dijsktra的变种代码是我看着自己打的,终于把代码和做法思路联系上了,也就是理解了算法——看来手跟着画一遍真的有助于理解. #define _CRT_SECURE_NO_WARNING ...
- DBSCAN算法
简单的说就是根据一个根据对象的密度不断扩展的过程的算法.一个对象O的密度可以用靠近O的对象数来判断.学习DBSCAN算法,需要弄清楚几个概念: 一:基本概念 1.:对象O的是与O为中心,为半径的空间, ...
- hdu 1352 I Conduit!
计算几何,主要是排序问题,其他都很好做…… ;}
- Android 解决ListView中每一项与button冲突
在listView的item里面如果有button,ImageButton等控件,会使得ListView不会被点击,解决方法是: ①在Button上面添加属性 android:focusable=&q ...
- Oracle的学习一:安装与卸载、sql *plus常用命令、Oracle用户管理
1.为什么学习oracle? 性能优越: 小型数据库 中型数据库 大型数据库 acess.foxbase mysql.sql server.informix sybase.oracle.db2 复杂量 ...
- cojs 简单的求和问题 解题报告
一个上午写了两个数据生成器,三个暴力和两个正解以及一个未竣工的伪正解思路 真是累死本宝宝了 首先这个题目暴力我的数据是有很多良心分的 但是不同的暴力拿到的分数也会有所差距,由于是题解就不说暴力怎么写了 ...
- Python批量读取人脸图片与数据互相转换
读取部分结果 程序 # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from PIL import ...