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 ...
随机推荐
- (2.1)mysql升级与降级
(2.1)mysql升级与降级 转自:深入浅出mysql数据库开发.优化与管理第二版 1.mysql升级 2.mysql降级
- vue js判断长按触发及手指的上滑、下滑、左滑、又滑
<span class="btn" @touchstart="touchstart()" @touchmove="touchmove()&quo ...
- vue/cli3 配置相对路径
根目录下新建 vue.config.js 文件 const path = require('path') function resolve(dir){ return path.join(__dirna ...
- PHP多线程 curl_multi_init 的使用
php中可以通过CURL处理HTTP请求,其中curl_init()是单线程模式,如果需要对事务处理走多线程模式,那么就需要用到curl_multi_init()函数. 本案例用来测试大并发的情况下 ...
- 『转载』判断一个正整数是不是素数,时间复杂度为O(根号n)
原文链接:https://blog.csdn.net/liangdagongjue/article/details/77895170#commentsedit PS:新手上路,实在找不到怎么转载,所以 ...
- 来测试下你的Java编程能力
上篇整理了下后面准备更系统化写的Java编程进阶的思路,如果仅看里面的词,很多同学会觉得都懂,但我真心觉得没有多少人是真懂的,所以简单的想了一些题目,感兴趣的同学们可以来做做看,看看自己的Java编程 ...
- ubuntu下zip文件操作
转自 https://blog.csdn.net/hpu11/article/details/71524013 .zip $ zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹 ...
- FB面经 Prepare: LCA of Deepest Nodes in Binary Tree
给一个 二叉树 , 求最深节点的最小公共父节点 . retrun . 先用 recursive , 很快写出来了, 要求用 iterative . 时间不够了... Recursion: 返回的时候返 ...
- flask hook
@app.before_first_requestdef before_first_request(): """在第一次请求之前会访问该函数""&qu ...
- windows CMD常用命令
命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一 ...