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 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.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode* fast = head;
struct ListNode* slow = head;
while(fast && fast->next){
fast = fast->next->next;
slow = slow ->next;
}
return slow;
}
leetcode-876 Middle of the 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 ...
- 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 ...
- LeetCode 876. Middle of the Linked List(获得链表中心结点)
题意:获得链表中心结点.当有两个中心结点时,返回第二个. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * ...
- 876. Middle of the Linked List - LeetCode
Question 876. Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完 ...
- 【Leetcode_easy】876. Middle of the Linked List
problem 876. Middle of the Linked List 参考 1. Leetcode_easy_876. Middle of the Linked List; 完
- 【LeetCode】876. Middle of the Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用哑结点 不使用哑结点 日期 题目地址:https ...
- [LeetCode&Python] Problem 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 ...
- 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 t ...
随机推荐
- Java_万年历(简单)
1.方法,需要一个年份,一个月份.然后在控制台输出日历 // 输入一个年份和一个月份显示日历 public static void printCalendar(int year, int month) ...
- SVN查看所有日志提交记录
1. svn默认显示最近一周的文件提交和修改记录,怎么查看更长时间的日志记录呢? 2. TortoiseSVN 3. 点击show all 或者NEXT 100,就可显示更长时间的文件提交记录.
- js-ES6学习笔记-修饰器
1.修饰器对类的行为的改变,是代码编译时发生的,而不是在运行时.这意味着,修饰器能在编译阶段运行代码. 2. function testable(target) { target.isTestable ...
- IDEA项目搭建三——简单配置Maven使用国内及本地仓库
大家在前面创建时发现Maven下载jar包的时候会很慢,我们又引入了自己的Maven,所以可以配置一下不让其去国外下载jar包,而使用国内的镜像站来加快下载速度 1.找到Maven所在文件夹 2.在c ...
- Nginx的常用功能
1.规范nginx的配置文件 在企业中我们的虚拟主机可能会很多,配置文件的内容也会有很多,这时候我们就可以规范一下我们的配置文件,把每个虚拟主机按照网站的域名或者是功能取名,放到统一的文件夹中,当然我 ...
- Android 常见adb命令
Android 常见adb命令 by:授客 QQ:1033553122 1. 查看所有已链接的设备 命令: adb devices 例: C:\Users\laiyu>adb device ...
- 团队项目个人进展——Day07
一.昨天工作总结 冲刺第七天,学习了微信小程序中WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接. 二.遇到的问题 对 ...
- LeetCode题解之 Continuous Subarray Sum
1.题目描述 2.循环计算即可 3.代码 bool checkSubarraySum(vector<int>& nums, int k) { ){ return false ; } ...
- 用JS实现判断iframe是否加载完成
本文出至:新太潮流网络博客 var iframe = document.createElement("iframe"); iframe.src = "blog.iinu. ...
- Solving the SQL Server Multiple Cascade Path Issue with a Trigger (转载)
Problem I am trying to use the ON DELETE CASCADE option when creating a foreign key on my database, ...