Lintcode225-Find Node in Linked List-Naive
2024-10-20 04:09:51
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后面放的是一个布尔表达式
最新文章
- ios 静态库冲突的解决办法
- panel的展开,关闭的一种应用。
- treeview所有节点递归解法(转+说明)或者说递归的实际应用
- SarePoint Powershell Add user to Group
- 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
- LearnMVC5-GettingStarted
- javascript --执行上下文,作用域
- Get file name without extension.
- poj1363
- Android项目svn代码管理问题
- 在CentOS 7下ISCSI和多路径部署文档【转】
- SDN学习之RYU源码安装
- bootstrap网站后台从设计到开发
- Spring Boot + Spring Cloud 实现权限管理系统 权限控制(Shiro 注解)
- http statusCode(状态码)含义
- d4
- ATX 安卓设备 WiFi 统一管理以及设备自动化测试
- 浅析ARM公司在物联网领域的战略布局
- 为什么你学不会递归?告别递归,谈谈我的一些经验 关于集合中一些常考的知识点总结 .net辗转java系列(一)视野 彻底理解cookie,session,token
- 6. 纯 CSS 绘制一颗闪闪发光的璀璨钻石
热门文章
- Windows —— cmd命令
- 3.1.3 Spring之AOP
- U盘安装Windows原版系统(安装方式有很多,我讲我的安装方式)
- MVC5+EF6 完整教程17--升级到EFCore2.0(转)
- 使用PL/SQL能查询oracle中数据,在for update 语句中一直卡住
- rinetd 通过公网连接云数据库
- centos上发布部署python的tornado网站项目完整流程
- (转)Oracle EBS 有效银行账户取值 银行科目
- 使用PHP实现RSA算法的加密和解密
- turtle画王思聪吃热狗(杨艳春,何金凝小组)