博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
228.链表的中点
阅读量:4697 次
发布时间:2019-06-09

本文共 1050 字,大约阅读时间需要 3 分钟。

Middle of Linked List

Description

Find the middle node of a linked list.

Example

Given 1->2->3, return the node with value 2.

Given 1->2, return the node with value 1.

Challenge

If the linked list is in a data stream, can you find the middle without iterating the linked list again?

/** * 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        if(head==null) return null;        ListNode slow=head;        ListNode fast=head;        while(fast.next!=null&&fast.next.next!=null){            slow=slow.next;            fast=fast.next.next;        }        return slow;    }}
描述找链表的中点。您在真实的面试中是否遇到过这个题?  样例链表 1->2->3 的中点是 2。链表 1->2 的中点是 1。挑战如果链表是一个数据流,你可以不重新遍历链表的情况下得到中点么?

转载于:https://www.cnblogs.com/browselife/p/10645972.html

你可能感兴趣的文章
C#结构体和类的区别
查看>>
模板 - 数论函数
查看>>
windows Api AlphaBlend的使用方法
查看>>
mysql主从延迟高的原因
查看>>
Leetcode 47. Permutations II
查看>>
DLL入门浅析【转】
查看>>
sql server:取当前时间前10分钟之内的数据 dateadd()
查看>>
python安装MySQLdb:出错Microsoft Visual C++ 9.0 is required
查看>>
BZOJ1027 [JSOI2007]合金 【计算几何 + floyd】
查看>>
【测绘图槽】03 测绘颂测绘人之歌(转载)
查看>>
LINUX下安装PHP(CGI模式)和NGINX[转]
查看>>
jQuery
查看>>
springboot定时器
查看>>
VS2017调试闪退之Chrome
查看>>
【Tip】如何让引用的dll随附的xml注释文档、pdb调试库等文件不出现在项目输出目录中...
查看>>
WPF中设置快捷键
查看>>
WebApi接口返回json,xml,text纯文本等
查看>>
C#/IOS/Android通用加密解密方法
查看>>
Web API 简单示例
查看>>
返璞归真 asp.net mvc (4) - View/ViewEngine
查看>>