225. Find Node in Linked List

Find a node with given value in a linked list. Return null if not exists.

Example

Example 1:

Input:  1->2->3 and value = 3
Output: The last node.

Example 2:

Input:  1->2->3 and value = 4
Output: null

Notice

If there are multiple nodes of the same value return the first one.

1. for循环

错误代码:

public ListNode findNode(ListNode head, int val) {
for (head; head != null; head = head.next) {
if (head.val == val) {
return head;
}
return null;
}
}

error: not a statement
for (head; head != null; head = head.next) {

for循环语法

for(初始化; 布尔表达式; 更新) {
//代码语句
}

最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。也就是说初始化的时候,必须声明循环变量的类型!

只写一个 head是错的,没有声明变量类型。

正确代码:

public ListNode findNode(ListNode head, int val) {
for (ListNode node = head; node != null; node = node.next) {
if (node.val == val) {
return node;
}
}
return null;
}

2. while循环

public ListNode findNode(ListNode head, int val) {
while(head != null) {
if (head.val == val) {
return head;
} else {
head = head.next;
}
}
return null;
}

while循环是直接用head,这是跟while的语法有关:

while( 布尔表达式 ) {
//循环内容
}

while后面放的是一个布尔表达式

Lintcode225-Find Node in Linked List-Naive的更多相关文章

  1. Solutions and Summay for Linked List Naive and Easy Questions

    1.Remove Linked List Elements package linkedlist; /* * Question: Remove all elements from a linked l ...

  2. remove Nth Node from linked list从链表中删除倒数第n个元素

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. [Swift]LeetCode1019. 链表中的下一个更大节点 | Next Greater Node In Linked List

    We are given a linked list with head as the first node.  Let's number the nodes in the list: node_1, ...

  4. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

  5. LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)

    题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayL ...

  6. leetcode1019 Next Greater Node In Linked List

    """ We are given a linked list with head as the first node. Let's number the nodes in ...

  7. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

  8. Leetcode 1019. Next Greater Node In Linked List

    单调栈的应用. class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: stack = [] ret = ...

  9. [Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

随机推荐

  1. Java中equals,hashcode,==的区别

    ==  :比较java栈局部变量表中变量的地址或值是否相等. equals : 比较变量的地址在java堆中引用对象是否为同一个对象. hashcode : 通过对象在JVM内存中的存储地址通过特定算 ...

  2. Jenkins+Jmeter持续集成笔记(五:问题优化)

    通过前面的一系列文章,我的API自动化测试平台已经搭建成型,但是要投入具体项目使用时,还有以下几个问题需要优化. 还是接着以上一篇笔记中的“test_token”项目为例: 1.邮件通知问题 (1)问 ...

  3. OpenDialog文件多选

    procedure TForm1.OpenFileListClick(Sender: TObject); var openDialog: TOpenDialog; I: Integer; begin ...

  4. Hadoop生态集群YARN详解

    一,前言 Hadoop 2.0由三个子系统组成,分别是HDFS.YARN和MapReduce,其中,YARN是一个崭新的资源管理系统,而MapReduce则只是运行在YARN上的一个应用,如果把YAR ...

  5. Azure架构(一):云计算基础

    云计算的定义 云计算(英语:cloud computing),是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需求提供给使用各种计算终端(桌面电脑.笔记本电脑.平板电脑.手机等) ...

  6. vue中动态样式不起作用? scoped了解一下

    vue中style标签使用属性scoped的注意事项 style上添加属性scoped可以实现样式私有化,但是在使用动态样式时,样式会不起作用.可以先去掉scoped

  7. 2019微信浏览器跳转外部浏览器下载app打开任意站实现方法

    很多朋友问我怎么解决微信内点击链接或扫描二维码可以直接跳出微信在外部浏览器打开网页链接和下载APP,其实这并不难,只要我们实现微信跳转功能即可.下面给大家介绍这个功能 方案实现教程: 功能目的 生成微 ...

  8. 蓝桥杯c/c++省赛真题——明码

    标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.###16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. ###一个字节可以存储8位信息,用3 ...

  9. 【JavaScript】事件捕获、事件冒泡与事件委托

    2018年12月18日 最近在学习js时,遇到了三个名词:事件捕获.事件冒泡.事件委托. 一.事件捕获和事件冒泡 事件捕获和事件冒泡是为了解决网页中的事件流(事件发生的顺序)而提出的概念. 事件捕获是 ...

  10. cygwin vim can't write .viminfo

    问题 每次退出vim时,都提示 vim can't wirte .viminfo 运行环境 以管理员身份登录win7,并运行cygwin 排查过程 切换到家目录,查看发现.viminfo文件存在. 查 ...