题目 203. 移除链表元素

删除链表中等于给定值 val 的所有节点。

题解

删除结点:要注意虚拟头节点。

代码

class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode pre= new ListNode(-1);
pre.next=head;
ListNode cur = pre;
while(cur.next!=null){
if(cur.next.val==val){//每次判断的是cur.next
cur.next=cur.next.next;
}else{//注意是else
cur=cur.next;
}
}
return pre.next;
}
}

题目 876. 链表的中间结点

给定一个带有头结点 head 的非空单链表,返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

题解

注意while中的条件,使得快指针是有效的。

代码

class Solution {
public ListNode middleNode(ListNode head) {
ListNode quick = head, slow= head;
while(quick!=null&&quick.next!=null){
quick=quick.next.next;
slow=slow.next;
}
return slow;
}
}

[LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)的更多相关文章

  1. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  2. lodash 移除数据元素 pull without 删除数组元素

    _.pull(array, [values]) 移除所有经过 SameValueZero 等值比较为 true 的元素 . without 不会修改原数组 <!DOCTYPE html> ...

  3. [LeetCode] 203. 移除链表元素

    题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/ 题目描述: 删除链表中等于给定值 val 的所有节点. 示例: 输 ...

  4. LeetCode 203——移除链表(JAVA)

    删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4 ...

  5. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  6. 【LeetCode】203.移除链表元素

    203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...

  7. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  8. LeetCode 203:移除链表元素 Remove LinkedList Elements

    删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...

  9. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

随机推荐

  1. SpringMVC常见问题Error configuring application listener of class org.springframework.web.context.ContextLoaderListenejava.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    六月 20, 2018 9:43:34 下午 org.apache.catalina.core.StandardContext listenerStart 严重: Error configuring ...

  2. python3 输出中文、日文等等乱码问题的解决办法

    例如: url = 'https://zozo.jp/shop/mrolive/goods-sale/44057773/?did=73037089' resp = requests.get(url=u ...

  3. Alink漫谈(十九) :源码解析 之 分位点离散化Quantile

    Alink漫谈(十九) :源码解析 之 分位点离散化Quantile 目录 Alink漫谈(十九) :源码解析 之 分位点离散化Quantile 0x00 摘要 0x01 背景概念 1.1 离散化 1 ...

  4. max user processes 导致的服务器大量close_wait问题解决过程

    1.背景: 由于现网业务量增长过快,需要扩容应用程序服务器,分担来自前端的访问压力. 2.故障: 部署好业务启动程序后,发现程序运行一小会后不产生新的日志和数据. 3.查问题过程: 1.首先查看程序运 ...

  5. Galera Cluster for MySQL 集群恢复

    node1: 1.rm -rf grastate.dat 2.mysqld_safe --wsrep-recover 3.galera_new_cluster node2: systemctl res ...

  6. 数字货币比特币以太坊买卖五档行情数据API接口

    数字货币比特币以太坊买卖五档行情数据API接口       数字货币一般包含比特币BTC.以太坊ETH.瑞波币XRP.泰达币USDT.比特币现金BCH.比特币SV.莱特币LTC.柚子币EOS.OKB. ...

  7. Leecode统计子串个数(java)

    /** 获取一个字符串在另一个字符串中出现的次数.判断str2在str1中出现的次数 */ public class StringExer2 { public static void main(Str ...

  8. Ubuntu18.04 解决umount: /mnt: device is busy

    通过该命令查看那个进程占用该device fuser -m /mnt 然后 kill -9 PID 最后就可以umount /mnt 了

  9. 一台主机的最大TCP连接数是多少?

    在没接触过这个问题之前,自然会想到服务器端连接数是由服务器端口号限制的.但这其实是一个很严重的误解,要解决这个问题,必须理解socket的连接过程. 以python为例,tcp服务端socket需要经 ...

  10. Java8中的Stream API

    本篇文章继续介绍Java 8的另一个新特性——Stream API.新增的Stream API与InputStream和OutputStream是完全不同的概念,Stream API是对Java中集合 ...