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. mysql (六)

    约束: 非空约束: 创建表时添加: create table stu( id  int , name  varchar(20) not null ) 创建完表之后 alter table stu  m ...

  2. 11.全局变量(static)

    1.数组 数组名是常量 2. 指针数组 4.局部变量 (1).作用域 作用的范围: (2).普通局部变量 在{}内定义: 只有执行到定义变量的这个语句,系统才会给这个变量分配空间. 当离开{},这个非 ...

  3. FEX(Fabric Extender)

    一.FEX Cisco Nexus 2000 FEX作为N5K.N6K.N7K.FI的一个远程线卡,单独的2K是没有网管功能的,必须配合父系交换机使用. 主要解决TOR和EOR的问题,TOR,接线简单 ...

  4. 【SpringBoot】springboot -- 2.0版本自定义ReidsCacheManager的改变

    1. 问题发现 在1.0版本中,我们配置redis的cacheManager是这种方式: //缓存管理器 @Bean public CacheManager cacheManager(@Suppres ...

  5. - configuration.module has an unknown property 'loader' 问题解决

    错误提示: Invalid configuration object. Webpack has been initialised using a configuration object that d ...

  6. 框架的一些bug问题记录

    关于java后台生成的mis表,在添加的add页面和edit页面中无法显示,去控制台里面查看.会有些错误的信息,是由于,你的那些html的标签不正常.导致无法显示. 里面有一个高级搜索,可以对这个选择 ...

  7. Backup &recovery备份和还原

    实践版本:MySQL5.7 备份类型(backup type)物理和逻辑备份(Physical Versus Logical Backup)        物理备份是指直接复制存储数据库内容的目录和文 ...

  8. IDEA搭建scala开发环境开发spark应用程序

    通过IDEA搭建scala开发环境开发spark应用程序   一.idea社区版安装scala插件 因为idea默认不支持scala开发环境,所以当需要使用idea搭建scala开发环境时,首先需要安 ...

  9. 蓝桥杯近3年初赛题之三(17年b组)

    1. 标题: 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购物单,都是 ...

  10. 小程序wx:for Do not set same key \"NaN\" in wx:key.

    在使用wx:for的时候出现了Do not set same key \"NaN\" in wx:key. 去网上查看资料,说是使用wx:key 试了一下,没用 字面意思是不要设置 ...