Lintcode225-Find Node in Linked List-Naive
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的更多相关文章
- 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 ...
- 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 ...
- [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, ...
- 【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 ...
- LeetCode 1019. Next Greater Node In Linked List (链表中的下一个更大节点)
题目标签:Linked List, Stack 题目给了我们一个 Linked List,让我们找出对于每一个数字,它的下一个更大的数字. 首先把 Linked List 里的数字 存入 ArrayL ...
- 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 ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- Leetcode 1019. Next Greater Node In Linked List
单调栈的应用. class Solution: def nextLargerNodes(self, head: ListNode) -> List[int]: stack = [] ret = ...
- [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 ...
随机推荐
- nginx修改上传文件大小限制
问题: 项目上线,图片上传报413错误,找了半天,原来是nginx限制了上传大小 在nginx.conf的server的location中加client_max_body_size 10m;
- innerHTML .innerText区别
().innerHtml("“):改变html元素: ().innerTEXT(”“):改变文本元素: 试验代码 <!DOCTYPE html> <html lang=&q ...
- 关于$ORACLE_HOME/bin/oracle文件属性
OS:AIX 7.1DB:12.1.0.2.0 RAC oracle@DB01:/home/oracle>sqlplus / as sysdba SQL*Plus: Release 12.1.0 ...
- PeopleSoft底层表,闪存查找历史代码(不小心改)
Oracle 闪存查找历史代码 select * from (SELECT * FROM PSPCMTXT AS OF TIMESTAMP to_timestamp('20180725 1 ...
- 接口自动化测试持续集成--Soapui接口功能测试参数化
按照自动化测试分层实现的原理,每一层的脚本实现都要进行参数化,自动化的目标就是要实现脚本代码与测试数据分离. 当测试数据进行调整的时候不会对脚本的实现带来震荡,从而提高脚本的稳定性与灵活度,降低脚本的 ...
- 最新阿里云申请免费SSL证书实现网站HTTPS化(图文教程一)
一.申请免费SSL证书: 1.登录阿里云: 2.领取代金券礼包: https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=0a ...
- WCF基础二
1:地址 . WCf 的每一个服务都有一个唯一地址,地址包括传输协议和服务位置. HTTP:采用http\https协议进行传输,默认端口为80. TCP:采用ne.tcp协议进行传输,默认端口为80 ...
- [转载来之雨松:NGUI研究院之为什么打开界面太慢(十三)]
本文固定链接: http://www.xuanyusong.com/archives/2799
- zw版足彩大数据&报价
zw版足彩大数据&报价 ::zw增强版足彩大数据,文件名后缀是'.dat' ::文件格式是标准文本格式,逗号分隔 ::zw增强版,在标准版赔率基础上,增加了倒数.比率两组归一化数据 ::zw版 ...
- 【转】Windons+jenkins,构建java程序,提示C:\Windows\TEMP\jenkins5037773887088486383.bat Access is denied
坑1: !!!前提:已设置本机电脑的账号密码, 解决方法:搜索程序services.msc-- 找到Jenkins-- 右键“属性”--登录--此账户--输入本机的账号密码--保存-- 停止Jenki ...