876. Middle of the Linked List【Easy】【单链表中点】
Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
Example 1:
Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
Example 2:
Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.
Note:
- The number of nodes in the given list will be between
1and100.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
if(head == null) return head;
ListNode slow = head, fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
876. Middle of the Linked List【Easy】【单链表中点】的更多相关文章
- [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 ...
- LeetCode 876. Middle of the Linked List(获得链表中心结点)
题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...
- 【Leetcode_easy】876. Middle of the Linked List
problem 876. Middle of the Linked List 参考 1. Leetcode_easy_876. Middle of the Linked List; 完
- 876. Middle of the Linked List - LeetCode
Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 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 链表的中间结点
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_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 ...
随机推荐
- Hive、Pig、HBase的关系与区别
欢迎关注大数据和人工智能技术文章发布的微信公众号:清研学堂,在这里你可以学到夜白(作者笔名)精心整理的笔记,让我们每天进步一点点,让优秀成为一种习惯! Pig 一种操作hadoop的轻量级脚本语言,最 ...
- Git版本回退的最佳方式
使用git开发的过程中,存在误提交的时候怎么办呢?不用慌张,强大的git提供了两种版本回退的方式,可以让你恢复提交之前的内容: 方式一:reset(不推荐) 通过reset的方式,把head指针指向之 ...
- 结合BeautyEye开源UI框架实现的较美观的Java桌面程序
BeautyJavaSwingRobot 结合BeautyEye开源UI框架实现的较美观的Java桌面程序,主要功能就是图灵机器人和一个2345网站万年历的抓取.... 挺简单而且实用的一个项目,实现 ...
- 【swupdate文档 二】许可证
许可证 SWUpdate是免费软件.它的版权属于Stefano Babic和其他许多贡献代码的人(详情请参阅实际源代码和git提交信息). 您可以根据自由软件基金会发布的GNU通用公共许可证第2版的条 ...
- 64_l6
lightdm-qt5-devel-1.22.0-1.fc26.i686.rpm 19-May-2017 11:11 22854 lightdm-qt5-devel-1.22.0-1.fc26.x86 ...
- 调用start()与run()的区别
1.调用start()方法: 通知“线程规划器”当前线程已经准备就绪,等待调用线程对象的run()方法.这个过程就是让系统安排一个时间来调用Thread中的run()方法,使线程得到运行,启动线程,具 ...
- Oracle 合并 merger into
merge into copy_emp1 c using employees e on (c.employee_id=e.employee_id)when matched then update ...
- 【COGS2622】后缀平衡树
这是个后缀平衡树的裸题.... 然后傻逼的我调了一下午. #include<bits/stdc++.h> typedef long long ll; using namespace std ...
- 获取file中字段,写入到TXT文件中
一下代码省略了很多,哈哈哈 a.txt文件 uid,type,pointx,pointy,name1,9,911233763,543857286,区间测速起点3,9,906371086,5453354 ...
- 解决 :java -version出现错误:“could not open `C:\Program Files\Java\jre7\lib\amd64\jvm.cfg”
cmd 下java -version出现错误:“could not open `C:\Program Files\Java\jre7\lib\amd64\jvm.cfg”,出现这种错误可能是由于先前有 ...