Middle of Linked List
Find the middle node of a linked list.
Given 1->2->3, return the node with value 2.
Given 1->2, return the node with value 1.
分析
1 value 1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * Definition for ListNode * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { /** * @param head: the head of linked list. * @return: a middle node of the linked list */ public ListNode middleNode(ListNode head) { // Write your code here ListNode slow = head, fast = head; while (fast != null && fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }} |
Middle of Linked List的更多相关文章
- Lintcode228-Middle of Linked List-Naive
228. Middle of Linked List Find the middle node of a linked list. Example Example 1: Input: 1->2- ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- linkedlist--lecture-4
1.链表数据结构 内存利用率高:动态分配 2.链表类定义 单向链表节点 public calss ListNode { int val =0; ListNode next = null; public ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
- [Swift]LeetCode876. 链表的中间结点 | Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- 876. Middle of the Linked List
1. 原始题目 Given a non-empty, singly linked list with head node head, return a middle node of linked li ...
- LeetCode 876 Middle of the Linked List 解题报告
题目要求 Given a non-empty, singly linked list with head node head, return a middle node of linked list. ...
- [LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
随机推荐
- autoreleasepool 自动释放池的理解
常见的面试题:以下代码存在什么样的问题?应该如何改进? for (int i = 0; i < 100000; i++) { NSString *str = @"abc"; ...
- SQL优化避免索引失效
Oracle 索引的目标是避免全表扫描,提高查询效率,但有些时候却适得其反.例如一张表中有上百万条数据,对某个字段加了索引,但是查询时性能并没有什么提高,这可 能是 oracle 索引失效造成的.or ...
- python-面向对象-内置方法补充
__del__item系列 __getitem__ __setitem__ __delitem____hash____eq__ 构造方法 申请一个空间析构方法 释放一个空间之前执行某对象借用了操作系统 ...
- Oracle的集合运算符
Oracle的集合运算符有并集union.union all,交集intersect,差集minus 先建表myemp,进行集合运算的测试 create table myemp as select * ...
- selenium webdriver API详解(三)
本系列主要讲解webdriver常用的API使用方法(注意:使用前请确认环境是否安装成功,浏览器驱动是否与谷歌浏览器版本对应) 一:获取页面元素的文本内容:text 例:获取我的博客名字文本内容 代码 ...
- 树形dp入门两题
题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...
- Hyperledger Fabric 1.0 从零开始(十三)——orderer分布式方案
简述 在搭建HyperLedger Fabric环境的过程中,我们会用到一个configtx.yaml文件(可参考Hyperledger Fabric 1.0 从零开始(八)——Fabric多节点集群 ...
- JAVA学习笔记--初识容器类库
一.前言 JAVA中一切皆为对象,因而,持有对象显得尤为重要. 在JAVA中,我们可以通过创建一个对象的引用的方式来持有对象: HoldingObject holding; 也可以创建一个对象数组来持 ...
- day-19 多种优化模型下的简单神经网络tensorflow示例
如下样例基于tensorflow实现了一个简单的3层深度学习入门框架程序,程序主要有如下特性: 1. 基于著名的MNIST手写数字集样例数据:http://yann.lecun.com/exdb/m ...
- Tim Cook在电话会议上宣布,Burberry前CEO Angela Ahrendts将在下周加入苹果
在今天的第二季度财报电话会议上,苹果公司的 CEO Tim Cook 宣布 Burberry 的前 CEO Angela Ahrendts 将在下周入职苹果,出任苹果负责零售和网上商店的高级副总裁. ...